Esempio n. 1
0
def BytecodeFromOpenClString(opencl_string: str,
                             optimization_level: str) -> str:
    """Create bytecode from OpenCL source string.

  Args:
    opencl_string: A string of OpenCL code.
    optimization_level: The optimization level to use, one of
        {-O0,-O1,-O2,-O3,-Ofast,-Os,-Oz}.

  Returns:
    A tuple of the arguments to clang, and the bytecode as a string.

  Raises:
    ClangException: If compiling to bytecode fails.
  """
    # Use -O3 to reduce CFGs.
    clang_args = opencl.GetClangArgs(use_shim=False) + [
        clang.ValidateOptimizationLevel(optimization_level),
        "-S",
        "-emit-llvm",
        "-o",
        "-",
        "-i",
        "-",
        "-Wno-everything",  # No warnings please.
    ]
    process = clang.Exec(clang_args, stdin=opencl_string)
    if process.returncode:
        raise clang.ClangException(
            "clang failed",
            returncode=process.returncode,
            stderr=process.stderr,
            command=clang_args,
        )
    return process.stdout, clang_args
Esempio n. 2
0
def BytecodeFromOpenClString(opencl_string: str) -> str:
    """Create bytecode from OpenCL source string.

  Args:
    opencl_string: A string of OpenCL code.

  Returns:
    The bytecode as a string.

  Raises:
    ClangException: If compiling to bytecode fails.
  """
    # Use -O3 to reduce CFGs.
    clang_args = opencl.GetClangArgs(
        use_shim=False) + ['-O3', '-S', '-emit-llvm', '-o', '-', '-i', '-']
    process = clang.Exec(clang_args, stdin=opencl_string)
    if process.returncode:
        raise clang.ClangException("clang failed with returncode "
                                   f"{process.returncode}:\n{process.stderr}")
    return process.stdout
Esempio n. 3
0
  ]
  process = clang.Exec(clang_args)
  if process.returncode:
<<<<<<< HEAD:deeplearning/ml4pl/ir/create/import_from_linux.py
<<<<<<< HEAD:deeplearning/ml4pl/ir/create/import_from_linux.py
    raise clang.ClangException(
<<<<<<< HEAD:deeplearning/ml4pl/ir/create/import_from_linux.py
      returncode=process.returncode, stderr=process.stderr, command=clang_args
    )
=======
        returncode=process.returncode,
        stderr=process.stderr,
        command=clang_args)
>>>>>>> 79a9a3551... Work in progress on graph model.:deeplearning/ml4pl/bytecode/create/import_from_linux.py
=======
    raise clang.ClangException(returncode=process.returncode,
                               stderr=process.stderr,
                               command=clang_args)
>>>>>>> 1acf38cb4... Automated code format.:deeplearning/ml4pl/bytecode/create/import_from_linux.py
=======
    raise clang.ClangException(
      returncode=process.returncode, stderr=process.stderr, command=clang_args
    )
>>>>>>> 4242aed2a... Automated code format.:deeplearning/ml4pl/bytecode/create/import_from_linux.py
  return process.stdout, clang_args


def TryToCreateControlFlowGraphsFromLinuxSrc(
  path: pathlib.Path,
) -> typing.List[cfg.ControlFlowGraph]:
  """Try to create CFGs from a Linux C source file.
Esempio n. 4
0
def BytecodeFromLinuxSrc(path: pathlib.Path, optimization_level: str) -> str:
  """Create bytecode from a Linux source file.

  Args:
    path: The path of the source file.
    optimization_level: The clang optimization level to use, one of
        {-O0,-O1,-O2,-O3,-Ofast,-Os,-Oz}.

  Returns:
    The bytecode as a string.

  Raises:
    ClangException: If compiling to bytecode fails.
  """
  root = linux.LinuxSourcesDataset().src_tree_root
  genroot = linux.LinuxSourcesDataset().generated_hdrs_root
  # A subset of the arguments found by running `make V=1` in the linux
  # build and grabbing a random C compile target.
  # The build I took this from: Wp,-MD,arch/x86/kernel/.asm-offsets.s.d  -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/5/include -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -fno-PIE -DCC_HAVE_ASM_GOTO -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -funit-at-a-time -DCONFIG_X86_X32_ABI -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_SSSE3=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -DCONFIG_AS_AVX512=1 -DCONFIG_AS_SHA1_NI=1 -DCONFIG_AS_SHA256_NI=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -DRETPOLINE -fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0 -Wframe-larger-than=1024 -fstack-protector-strong -Wno-unused-but-set-variable -fno-var-tracking-assignments -g -gdwarf-4 -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fno-merge-all-constants -fmerge-constants -fno-stack-check -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init    -DKBUILD_BASENAME='"asm_offsets"' -DKBUILD_MODNAME='"asm_offsets"'  -fverbose-asm -S -o arch/x86/kernel/asm-offsets.s arch/x86/kernel/asm-offsets.c
  clang_args = [
    "-S",
    "-emit-llvm",
    "-o",
    "-",
    clang.ValidateOptimizationLevel(optimization_level),
    "-Wno-everything",  # No warnings please.
    "-I",
    str(root / "arch/x86/include"),
    "-I",
    str(genroot / "arch/x86/include/generated"),
    "-I",
    str(root / "include"),
    "-I",
    str(root / "arch/x86/include/uapi"),
    "-I",
    str(genroot / "arch/x86/include/generated/uapi"),
    "-I",
    str(root / "include/uapi"),
    "-I",
    str(genroot / "include/generated/uapi"),
    "-I",
    str(genroot / "arch/x86/include"),
    "-I",
    str(genroot / "arch/x86/include/generated"),
    "-I",
    str(genroot / "arch/x86/include/generated/uapi"),
    "-I",
    str(genroot / "include"),
    "-I",
    str(genroot / "include/generated"),
    "-include",
    str(genroot / "include/linux/kconfig.h"),
    "-include",
    str(genroot / "include/linux/compiler_types.h"),
    "-D__KERNEL__",
    "-m64",
    "-DCONFIG_X86_X32_ABI",
    "-DCONFIG_AS_CFI=1",
    "-DCONFIG_AS_CFI_SIGNAL_FRAME=1",
    "-DCONFIG_AS_CFI_SECTIONS=1",
    "-DCONFIG_AS_FXSAVEQ=1",
    "-DCONFIG_AS_SSSE3=1",
    "-DCONFIG_AS_CRC32=1",
    "-DCONFIG_AS_AVX=1",
    "-DCONFIG_AS_AVX2=1",
    "-DCONFIG_AS_AVX512=1",
    "-DCONFIG_AS_SHA1_NI=1",
    "-DCONFIG_AS_SHA256_NI=1",
    "-pipe",
    "-DRETPOLINE",
    "-DCC_USING_FENTRY",
    "-DKBUILD_BASENAME='\"asm_offsets\"'",
    "-DKBUILD_MODNAME='\"asm_offsets\"'",
    str(path),
  ]
  process = clang.Exec(clang_args)
  if process.returncode:
    raise clang.ClangException(
      returncode=process.returncode, stderr=process.stderr, command=clang_args
    )
  return process.stdout, clang_args