Beispiel #1
0
def buildLocal(benchmark):
    CONTECH_HOME = util.findContechInstall()
    os.chdir(os.path.join(CONTECH_HOME, "scripts"))
    try:
        util.pcall(["./build_parsec.py {0}".format(benchmark)])
    except:
        pass
Beispiel #2
0
def buildLocal(benchmark):
    CONTECH_HOME = util.findContechInstall()
    os.chdir(os.path.join(CONTECH_HOME, "scripts"))
    try:
        util.pcall(["./build_parsec.py {0}".format(benchmark)])
    except:
        pass
Beispiel #3
0
def main():
    parser = argparse.ArgumentParser(
        description="Builds a nas benchmark with contech")
    parser.add_argument("benchmark", help="The nas benchmark to build.")
    parser.add_argument("-i",
                        "--input",
                        help="The input size to use.",
                        default="S")
    parser.add_argument("-c",
                        "--config",
                        help="The configuration to use.",
                        default="contech")

    args = parser.parse_args()

    if os.environ.has_key("CONTECH_HOME"):
        CONTECH_HOME = os.environ["CONTECH_HOME"]
        stateFile = os.path.join(CONTECH_HOME, "scripts/output/",
                                 args.benchmark + ".stateFile.temp")
        os.environ["CONTECH_STATE_FILE"] = stateFile
    else:
        print ">Error: Could not find contech installation. Set CONTECH_HOME to the root of your contech directory."
        exit(1)

    if os.environ.has_key("NAS_HOME"):
        NAS_HOME = os.environ["NAS_HOME"]
    else:
        print ">Error: Could not find NAS installation. Set NAS_HOME to the root of your NAS directory."
        exit(1)

    # Prepare state file for run
    if os.path.exists(stateFile):
        os.remove(stateFile)

    with Timer("Build"):
        #Change directory to NAS_HOME
        savedPath = os.getcwd()
        os.chdir(NAS_HOME)
        pcall(
            ["rm -f {0}/*.o {0}/*.ll {0}/*.bc".format(args.benchmark.upper())])
        returnCode = pcall([
            "make", args.benchmark, "CLASS={}".format(args.input),
            "CONFIG={}.def".format(args.config)
        ],
                           returnCode=True)
        os.chdir(savedPath)

    # Clean up
    if os.path.exists(stateFile):
        os.remove(stateFile)

    if returnCode != 0:
        util.print_error("Build failed")
        exit(1)
def main():
    CONTECH_HOME = util.findContechInstall()
    CONTECH_WRAPPER = os.path.join(CONTECH_HOME, "scripts/contech_wrapper.py")
    CONTECH_MARKER = os.path.join(CONTECH_HOME, "scripts/contech_marker.py")
    stateFile = os.path.join(CONTECH_HOME, "scripts/output/contechStateFile.temp")
    
    OBJPARSE = os.path.join(CONTECH_HOME, "scripts/objparse.py")
    RUN = os.path.join(CONTECH_HOME, "scripts/run.py")

    parser = argparse.ArgumentParser(description='Generates a task graph with a matching marked object file for a parsec benchmark')
    parser.add_argument('benchmark', help='The simple benchmark to build.')
    args = parser.parse_args()

    # Reset state file
    if os.path.exists(stateFile):
        os.remove(stateFile)
    
    # Marker run
    pcall([CONTECH_MARKER, args.benchmark + ".c", "-o", args.benchmark, "-lpthread", "-O3"])
    
    # Objparse
    markedObjectFile = os.path.join(CONTECH_HOME, "middle/output/" + os.path.basename(args.benchmark) + ".mo")
    pcall([OBJPARSE, args.benchmark, markedObjectFile])
    
    # Reset state file
    os.remove(stateFile)
    
    # Actual build
    pcall([CONTECH_WRAPPER, args.benchmark + ".c", "-o", args.benchmark, "-lpthread", "-O3"])
    
    # Generate taskgraph
    pcall([RUN, args.benchmark])
Beispiel #5
0
def main():
	parser = argparse.ArgumentParser(description="Builds a parsec benchmark with contech")
	parser.add_argument("benchmark", help="The parsec bencmark to build.")
	parser.add_argument("--bldconf", default="contech", help="PARSEC build configuration (bldconf) to use for building.")
	parser.add_argument("--hammerOptLevel", help="Set hammer optimization level (bldconf==hammer only)")
	args = parser.parse_args()
	
	if os.environ.has_key("CONTECH_HOME"):
		CONTECH_HOME = os.environ["CONTECH_HOME"]
		stateFile = os.path.join(CONTECH_HOME, "scripts/output/", args.benchmark + ".stateFile.temp")
		os.environ["CONTECH_STATE_FILE"] = stateFile
	else:
		print ">Error: Could not find contech installation. Set CONTECH_HOME to the root of your contech directory."
		exit(1)

	if os.environ.has_key("PARSEC_HOME"):
		PARSEC_HOME = os.environ["PARSEC_HOME"]
		PARSECMGMT = os.path.join(PARSEC_HOME, "bin/parsecmgmt")
	else:
		print ">Error: Could not find parsec installation. Set PARSEC_HOME to the root of your parsec directory."
		exit(1)


	# Run the parsec benchmark
	print ">Building " + args.benchmark
	
	# Prepare state file for run
	if os.path.exists(stateFile):
		os.remove(stateFile)
		
	# Hammer: Prepare hammer nail file 
	if args.bldconf == "hammer":
		os.environ["HAMMER_NAIL_FILE"] = os.path.join(CONTECH_HOME, "backend/Hammer/compilerData/{}.bin".format(args.benchmark))
		os.environ["HAMMER_OPT_LEVEL"] = args.hammerOptLevel
		
	pcall([PARSECMGMT, "-a", "uninstall", "-p", args.benchmark, "-c", args.bldconf])
	with Timer("Build"):
		returnCode = pcall([PARSECMGMT, "-a", "build", "-p", args.benchmark, "-c", args.bldconf], returnCode=True)
	
	# Clean up
	if os.path.exists(stateFile):
		os.remove(stateFile)
	elif args.bldconf in ["contech", "contech_marker", "hammer"]: 
		util.print_warning("Warning: no statefile was generated.")
	
	if returnCode != 0:
		util.print_error("Build failed")
		exit(1)
Beispiel #6
0
def main():
    parser = argparse.ArgumentParser(description="Builds a nas benchmark with contech")
    parser.add_argument("benchmark", help="The nas benchmark to build.")
    parser.add_argument("-i", "--input", help="The input size to use.", default="S")
    parser.add_argument("-c", "--config", help="The configuration to use.", default="contech")
    
    args = parser.parse_args()
    
    if os.environ.has_key("CONTECH_HOME"):
        CONTECH_HOME = os.environ["CONTECH_HOME"]
        stateFile = os.path.join(CONTECH_HOME, "scripts/output/", args.benchmark + ".stateFile.temp")
        os.environ["CONTECH_STATE_FILE"] = stateFile
    else:
        print ">Error: Could not find contech installation. Set CONTECH_HOME to the root of your contech directory."
        exit(1)

    if os.environ.has_key("NAS_HOME"):
        NAS_HOME = os.environ["NAS_HOME"]
    else:
        print ">Error: Could not find NAS installation. Set NAS_HOME to the root of your NAS directory."
        exit(1)
    
    # Prepare state file for run
    if os.path.exists(stateFile):
        os.remove(stateFile)
        
    with Timer("Build"):
        #Change directory to NAS_HOME
        savedPath = os.getcwd()
        os.chdir(NAS_HOME)
        pcall(["rm -f {0}/*.o {0}/*.ll {0}/*.bc".format(args.benchmark.upper())])
        returnCode = pcall(["make", args.benchmark, "CLASS={}".format(args.input), "CONFIG={}.def".format(args.config)], returnCode=True)
        os.chdir(savedPath)
    
    # Clean up
    if os.path.exists(stateFile):
        os.remove(stateFile)
    
    if returnCode != 0:
        util.print_error("Build failed")
        exit(1)
Beispiel #7
0
def main():
	CONTECH_HOME = util.findContechInstall()
	OBJPARSE = os.path.join(CONTECH_HOME, "scripts/objparse.py")

	PARSEC_HOME = util.findParsecInstall()
	PARSECMGMT = os.path.join(PARSEC_HOME, "bin/parsecmgmt")


	parser = argparse.ArgumentParser(description='Generates a marked object file (.mo) for a parsec benchmark')
	parser.add_argument('benchmark', help='The parsec benchmark.')
	args = parser.parse_args()

	stateFile = os.path.join(CONTECH_HOME, "scripts/output/", args.benchmark + ".stateFile.temp")
	os.environ["CONTECH_STATE_FILE"] = stateFile
	
	# Run the parsec benchmark
	print ">Building " + args.benchmark
	
	# Prepare state file for run
	if os.path.exists(stateFile):
		os.remove(stateFile)
	
	pcall([PARSECMGMT, "-a", "uninstall", "-p", args.benchmark, "-c", "contech_marker"])
	pcall([PARSECMGMT, "-a", "build", "-p", args.benchmark, "-c", "contech_marker"])
		
	# Find the executable generated by the build
	exe = os.path.join(PARSEC_HOME, "pkgs/apps/" + args.benchmark + "/inst/amd64-linux.contech_marker/bin/" + args.benchmark)
	# Parse the code to generate a marked object file
	markedObjectFile = os.path.join(CONTECH_HOME, "middle/output/" + args.benchmark + ".mo")
	pcall([OBJPARSE, exe, markedObjectFile])
		
	# Clean up
	os.remove(stateFile)
Beispiel #8
0
def main():
    CONTECH_HOME = util.findContechInstall()
    CONTECH_WRAPPER = os.path.join(CONTECH_HOME, "scripts/contech_wrapper.py")
    CONTECH_MARKER = os.path.join(CONTECH_HOME, "scripts/contech_marker.py")
    stateFile = os.path.join(CONTECH_HOME,
                             "scripts/output/contechStateFile.temp")

    OBJPARSE = os.path.join(CONTECH_HOME, "scripts/objparse.py")
    RUN = os.path.join(CONTECH_HOME, "scripts/run.py")

    parser = argparse.ArgumentParser(
        description=
        'Generates a task graph with a matching marked object file for a parsec benchmark'
    )
    parser.add_argument('benchmark', help='The simple benchmark to build.')
    args = parser.parse_args()

    # Reset state file
    if os.path.exists(stateFile):
        os.remove(stateFile)

    # Marker run
    pcall([
        CONTECH_MARKER, args.benchmark + ".c", "-o", args.benchmark,
        "-lpthread", "-O3"
    ])

    # Objparse
    markedObjectFile = os.path.join(
        CONTECH_HOME,
        "middle/output/" + os.path.basename(args.benchmark) + ".mo")
    pcall([OBJPARSE, args.benchmark, markedObjectFile])

    # Reset state file
    os.remove(stateFile)

    # Actual build
    pcall([
        CONTECH_WRAPPER, args.benchmark + ".c", "-o", args.benchmark,
        "-lpthread", "-O3"
    ])

    # Generate taskgraph
    pcall([RUN, args.benchmark])
Beispiel #9
0
def main():
    CONTECH_HOME = util.findContechInstall()
    OBJPARSE = os.path.join(CONTECH_HOME, "scripts/objparse.py")

    PARSEC_HOME = util.findParsecInstall()
    PARSECMGMT = os.path.join(PARSEC_HOME, "bin/parsecmgmt")

    parser = argparse.ArgumentParser(
        description=
        'Generates a marked object file (.mo) for a parsec benchmark')
    parser.add_argument('benchmark', help='The parsec benchmark.')
    args = parser.parse_args()

    stateFile = os.path.join(CONTECH_HOME, "scripts/output/",
                             args.benchmark + ".stateFile.temp")
    os.environ["CONTECH_STATE_FILE"] = stateFile

    # Run the parsec benchmark
    print ">Building " + args.benchmark

    # Prepare state file for run
    if os.path.exists(stateFile):
        os.remove(stateFile)

    pcall([
        PARSECMGMT, "-a", "uninstall", "-p", args.benchmark, "-c",
        "contech_marker"
    ])
    pcall([
        PARSECMGMT, "-a", "build", "-p", args.benchmark, "-c", "contech_marker"
    ])

    # Find the executable generated by the build
    exe = os.path.join(
        PARSEC_HOME, "pkgs/apps/" + args.benchmark +
        "/inst/amd64-linux.contech_marker/bin/" + args.benchmark)
    # Parse the code to generate a marked object file
    markedObjectFile = os.path.join(CONTECH_HOME,
                                    "middle/output/" + args.benchmark + ".mo")
    pcall([OBJPARSE, exe, markedObjectFile])

    # Clean up
    os.remove(stateFile)
Beispiel #10
0
def main(arg):
    # TODO: usage should be based on argparse
    if (len(arg)) == 1:
        print "Usage: {0} input\n".format(arg[0])
        exit()

    parser = argparse.ArgumentParser(
        description="Runs dynamic analysis backend on benchmark")
    parser.add_argument("-b", "--bitcode", help="Bitcode file to analyze")
    parser.add_argument("-t", "--taskgraph", help="Taskgraph file to analyze")
    parser.add_argument("-o", default="output", help="Output directory")
    parser.add_argument("-n",
                        default="65",
                        help="Number of contexts to analyze")
    args = parser.parse_args()

    DynAnalysisLib = "-load=lib/LLVMDynAnalysis.so"
    ExecLatency = "-execution-units-latency={1,2,3,1,3,5,6,1,4,4,12,12,30,30,100,100}"
    ROBSize = "-reorder-buffer-size=168"
    LBSize = "-load-buffer-size=64"
    SBSize = "-store-buffer-size=36"
    CacheLineSize = "-cache-line-size=64"
    ILP = "-instruction-fetch-bandwidth=4"
    RSSize = "-reservation-station-size=56"
    L1Size = "-l1-cache-size=32768"
    L2Size = "-l2-cache-size=262144"
    LLCSize = "-llc-cache-size=20971520"
    ExecIssue = "-execution-units-parallel-issue={1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1}"
    ExecThru = "-execution-units-throughput={3,1,1,1,1,1,1,1,8,8,32,32,32,32,8,8}"
    FillBufSize = "-line-fill-buffer-size=10"
    WordSize = "-memory-word-size=8"
    AccessGran = "-mem-access-granularity={8,8,64,64,64,64,64,64}"

    # Assume that the taskgraph is formatted according to regress / util script convention
    benchName = args.taskgraph.split('/')[-1]
    b = benchName.split('.')

    if (b[0] == "splash2x"):
        benchName = b[1]
    else:
        benchName = b[0]

    try:
        os.makedirs(args.o + "/" + benchName)
    except os.error:
        #path already exists
        arg = arg

    for ctid in range(int(args.n)):
        filename = args.o + "/" + benchName + "/ct_{0}".format(ctid)
        ofile = tempfile.NamedTemporaryFile(suffix=".bc")
        with open(filename, "w") as of:
            #Use bash to expand arguments per https://stackoverflow.com/questions/8945826/expand-shell-path-expression-in-python
            util.pcall([
                "bash -c \" opt", DynAnalysisLib, "-EnginePass", args.bitcode,
                "-taskgraph-file=" + args.taskgraph, "-o " + ofile.name,
                "-context-number={0}".format(ctid), ExecLatency, ROBSize,
                LBSize, SBSize, CacheLineSize, ILP, RSSize, L1Size, L2Size,
                LLCSize, ExecIssue, ExecThru, FillBufSize, WordSize,
                AccessGran, "\""
            ],
                       outputFile=of,
                       suppressOutput=False,
                       silent=False)
        ofile.close()
        try:
            os.unlink(ofile.name)
        except os.error:
            # we expect that there will be an error returned by the unlink
            #   The file should already be deleted, but we are calling just in case
            arg = arg
Beispiel #11
0
def passThrough(CC):
    command = [CC] + sys.argv[1:]
    pcall(command, silent=True)
    exit(0)
Beispiel #12
0
def main(isCpp=False, markOnly=False, minimal=False, hammer=False):

    # Hack to detect on ARM
    ARM = False
    if os.environ.has_key("USER"):
        uname = os.environ["USER"]
        if uname == "ubuntu":
            ARM = True

    # Set locations of clang, opt, and the contech pass
    if os.environ.has_key("CONTECH_HOME"):
        CONTECH_HOME = os.environ["CONTECH_HOME"]
        if os.environ.has_key("CONTECH_LLVM_HOME"):
            CONTECH_LLVM_HOME = os.environ["CONTECH_LLVM_HOME"]
            LLVMCONTECH = CONTECH_LLVM_HOME + "/lib/LLVMContech.so"
            CLANG = CONTECH_LLVM_HOME + "/bin/clang"
            CLANGPP = CLANG + "++"
            OPT = CONTECH_LLVM_HOME + "/bin/opt"
        else:
            LLVMCONTECH = CONTECH_HOME + "/llvm-contech/lib/LLVMContech.so"
            CLANG = "clang"
            CLANGPP = "clang++"
            OPT = "opt"
        if markOnly:
            # Use the .o file so that LLVM does not optimize away the marker calls
            RUNTIME = CONTECH_HOME + "/common/runtime/ct_runtime.o"
        else:
            if ARM == True:
                RUNTIME = CONTECH_HOME + "/common/runtime/ct_runtime.bc " + CONTECH_HOME + "/common/runtime/ct_main.bc "
            else:
                RUNTIME = CONTECH_HOME + "/common/runtime/ct_runtime.bc " + CONTECH_HOME + "/common/runtime/ct_main.bc "

        if os.environ.has_key("CONTECH_STATE_FILE"):
            stateFile = os.environ["CONTECH_STATE_FILE"]
        else:
            stateFile = CONTECH_HOME + "/scripts/output/contechStateFile.temp"

    else:
        print ">Error: Could not find contech installation. Set CONTECH_HOME to the root of your contech directory."
        exit(1)

    #LOCAL = "/net/tinker/local"
    #LLVMCONTECH = LOCAL + "/lib/LLVMContech.so"
    #RUNTIME = LOCAL + "/lib/libct_runtime.a"

    # Name of the .c file to be processed
    cfile = ""
    # Names of the .o files to be linked
    ofiles = ""
    # .o files hiding in .a files to be linked
    oFromAFiles = ""
    # Name of the output file
    out = ""
    # All remaining flags to be passed on to clang
    if ARM == True:
        CFLAGS = "--verbose -pthread"
    else:
        CFLAGS = "--verbose -pthread"

    # Choose correct compiler
    if isCpp:
        CC = CLANGPP
    else:
        CC = CLANG

    outFileComingNext = False
    compileOnly = False
    depsOnly = False
    dragon = False
    MPI = False

    for arg in sys.argv[1:]:

        # This compile step is just generating dependencies, don't compile or link unless instructed to
        if "-M" in arg:
            depsOnly = True

        # -o, look for filename next
        elif "-o" == arg:
            outFileComingNext = True

        # -o attached to other options
        elif "-o," in arg and out == "":

            # Option might have been passed to the linker with something like -Wl,-o,out
            if "," in arg:
                for token in arg.split(","):
                    if outFileComingNext == True:
                        out = token
                        outFileComingNext = False
                    elif token == "-o":
                        outFileComingNext = True
                # Make sure the whole option makes it to the CFLAGS, just in case
                CFLAGS = CFLAGS + " " + arg

            # -o attached to the front of the name
            else:
                out = arg
                out = out.replace("-o", "", 1)
            # TODO What other horrible ways are there to pass the name of the output file???

        # Saw -o at last arg
        elif outFileComingNext:
            out = arg
            outFileComingNext = False

        # C++ file
        elif ".cxx" == arg[-4:]:
            cfile = arg
            isCpp = True
        elif ".cpp" == arg[-4:]:
            cfile = arg
            isCpp = True
        elif ".cc" == arg[-3:]:
            cfile = arg
            isCpp = True
        # C file
        elif ".c" == arg[-2:]:
            cfile = arg
            isCpp = False
        elif ".f" == arg[-2:]:
            # Fortran only supported through dragonEgg and not clang
            cfile = arg
            dragon = True
        # Object file
        elif ".o" == arg[-2:]:
            ofiles = ofiles + " " + arg

        # Compile only
        elif "-c" == arg:
            compileOnly = True
        elif "-lmpi" == arg[0:5]:
            MPI = True
            CFLAGS = CFLAGS + " " + arg
        elif "-fcilkplus" == arg:
            CFLAGS = CFLAGS + " " + arg + " "
            #CC = CC + "-cilk"
        # Combine other args into CFLAGS
        else:
            CFLAGS = CFLAGS + " " + arg

    # Debug
    #print ""
    #print "Contech wrapper: "
    #print "cfile=" + cfile
    #print "ofiles=" + ofiles
    #print "CFLAGS=" + CFLAGS
    #print "out=" + outdo

    # Found some flag that we don't handle, just pass through the compiler and exit
    if depsOnly and not compileOnly:
        passThrough(CC)

    # Compile requested but no input found, let the compiler throw an error
    if cfile == "" and compileOnly:
        passThrough(CC)

    # Requires dragon egg
    elif dragon == True:
        name = cfile[0:len(cfile) - 2]

        # Define names of intermediate files
        A = name + ".ll"
        B = name + "_ct.bc"

        # Define name of compiled file
        newobj = ""
        if out != "":
            newobj = out
        else:
            newobj = name + ".o"

        # Make sure the output ends in .o
        if newobj[-2:] != ".o":
            newobj = newobj + ".o"

        pcall([
            "gcc", CFLAGS, "-S",
            "-fplugin=" + CONTECH_LLVM_HOME + "/lib/dragonegg.so",
            "-fplugin-arg-dragonegg-emit-ir", cfile, "-o", newobj
        ])
        ofiles = ofiles + " " + newobj

    # Input file found, assuming compile requested.
    # Compile with contech
    elif cfile != "":
        newobj = ""
        if out != "":
            newobj = out
        else:
            # Get the name of cfile without an extension
            if isCpp:
                name = cfile.replace(".cpp", "", 1)
                name = name.replace(".cc", "", 1)
            else:
                #linux.contech ...
                name = cfile[0:len(cfile) - 2]
            newobj = name + ".o"
        if newobj[-2:] != ".o":
            newobj = newobj + ".o"
        pcall([CC, CFLAGS, cfile, "-emit-llvm", "-c", "-o", newobj])
        ofiles = ofiles + " " + newobj
    # Link
    if not compileOnly:
        if ofiles != "":

            # Define name of final executable
            if out == "":
                out = "a.out"
            # Does the binary use MPI?
            if MPI == True:
                RUNTIME = RUNTIME + CONTECH_HOME + "/common/runtime/ct_mpi.bc "
            else:
                RUNTIME = RUNTIME + CONTECH_HOME + "/common/runtime/ct_nompi.bc "

            # Compile final executable

            # But first extract .o files hiding in .a libraries
            TMPARDIR = tempfile.mkdtemp()
            oFromARaw = ""
            oAltLib = ""
            if os.path.isdir(TMPARDIR):
                pcall(["mkdir", "-p", TMPARDIR])

                remFlags = []
                for flag in CFLAGS.split():
                    if ".a" == flag[-2:]:
                        aFilename = flag
                        if not flag.find("/") == -1:
                            aFilename = flag.split("/")[-1]
                        pcall(["cp", flag, TMPARDIR])
                        pcall([
                            "cd", TMPARDIR, ";",
                            CONTECH_HOME + "/scripts/uniq_ar_x.sh", aFilename
                        ])
                        remFlags.append(flag)
                for f in remFlags:
                    print "Remove: " + f + "\t from: " + CFLAGS
                    CFLAGS = CFLAGS.replace(f, "")
                oFromA = glob.glob(TMPARDIR + "/*.o")
                for f in oFromA:
                    head = subprocess.Popen(["head", "-c", "3", f],
                                            stdout=subprocess.PIPE)
                    oFromAType = head.stdout.read()
                    if oFromAType.startswith("BC"):
                        oFromAFiles = oFromAFiles + " " + f
                    else:
                        oFromARaw = oFromARaw + " " + f

                # As the result of link will be instrumented, do not include RUNTIME, etc
                pcall([
                    "llvm-link", "-v", ofiles, oFromAFiles, "-o",
                    out + ".link.bc"
                ])
                pcall([
                    OPT, "-load=" + LLVMCONTECH, "-Contech", out + ".link.bc",
                    "-o", out + "_ct.link.bc", "-ContechState", stateFile
                ])

                if oFromARaw != "":
                    pcall(["ar", "crsu", "ct_" + remFlags[0], oFromARaw])
                    oAltLib = oAltLib + " " + "ct_" + remFlags[0]

                pcall(["rm", "-rf", TMPARDIR])

            # Link in basic block table
            shutil.copyfile(stateFile, "contech.bin")
            # Note that we may have to create two .o, one for 32bit and one for 64bit
            OBJCOPY = "objcopy"

            if ARM == True:
                pcall([
                    OBJCOPY, "--input binary", "--output elf32-littlearm",
                    "--binary-architecture arm", "contech.bin",
                    "contech_state.o"
                ])
            else:
                pcall([
                    OBJCOPY, "--input binary", "--output elf64-x86-64",
                    "--binary-architecture i386", "contech.bin",
                    "contech_state.o"
                ])
                #pcall([OBJCOPY, "--input binary", "--output elf32-i386", "--binary-architecture i386", "contech.bin", "contech_state.o"])

            if ARM == True:
                pcall([
                    OPT, "-always-inline", out + "_ct.link.bc", "-o",
                    out + "_ct_inline.bc"
                ])
                pcall([
                    CC, CFLAGS, "-c -o", out + "_ct.o", out + "_ct_inline.bc"
                ])
                pcall([
                    CC, out + "_ct.o", CFLAGS, "-o", out, "-lpthread",
                    "contech_state.o"
                ])
            else:
                #Cilk runtime requires -ldl?
                #Contech runtime requires -lrt and -lpthread
                pcall([
                    CC, "-flto", oAltLib, out + "_ct.link.bc", oAltLib,
                    RUNTIME, CFLAGS, "-o", out, "-lrt", "-ldl", "-lpthread",
                    "contech_state.o"
                ])
        else:
            passThrough(CC)
Beispiel #13
0
def passThrough(CC):
    command = [CC] + sys.argv[1:] 
    pcall(command, silent=True)
    exit(0)
Beispiel #14
0
def main(isCpp = False, markOnly = False, minimal = False, hammer = False):
    
    # Hack to detect on ARM
    ARM = False
    if os.environ.has_key("USER"):
        uname = os.environ["USER"]
        if uname == "ubuntu":
            ARM = True
    
    # Set locations of clang, opt, and the contech pass
    if os.environ.has_key("CONTECH_HOME"):
        CONTECH_HOME = os.environ["CONTECH_HOME"]
        #CLANG = CONTECH_HOME + "/llvm_fe_3.2/build/Release+Asserts/bin/clang"
        #CLANGPP = CONTECH_HOME + "/llvm_fe_3.2/build/Release+Asserts/bin/clang++"
        #OPT = CONTECH_HOME + "/llvm_fe_3.2/build/Release+Asserts/bin/opt"
        CT_FILE = CONTECH_HOME + "/common/taskLib/ct_file_C.o"
        if os.environ.has_key("CONTECH_LLVM_HOME"):
            CONTECH_LLVM_HOME = os.environ["CONTECH_LLVM_HOME"]
            LLVMCONTECH = CONTECH_LLVM_HOME + "/lib/LLVMContech.so"
            CLANG = CONTECH_LLVM_HOME + "/bin/clang"
            CLANGPP = CLANG + "++"
            OPT = CONTECH_LLVM_HOME + "/bin/opt"
        else:
            LLVMCONTECH = CONTECH_HOME + "/llvm_fe_3.2/build/Release+Asserts/lib/LLVMContech.so"
            CLANG = "clang"
            CLANGPP = "clang++"
            OPT = "opt"
        LLVMHAMMER = CONTECH_HOME + "/llvm_fe_3.2/build/Release+Asserts/lib/LLVMHammer.so"
        #RUNTIME = CONTECH_HOME + "/common/runtime/libct_runtime.a"
        if markOnly:
            # Use the .o file so that LLVM does not optimize away the marker calls
            RUNTIME = CONTECH_HOME + "/common/runtime/ct_runtime.o"
        else:
            if ARM == True:
                RUNTIME = CONTECH_HOME + "/common/runtime/ct_runtime.bc " + CONTECH_HOME + "/common/runtime/ct_main.bc "
            else:
                RUNTIME = CONTECH_HOME + "/common/runtime/ct_runtime.bc " + CONTECH_HOME + "/common/runtime/ct_main.bc "
        
        if os.environ.has_key("CONTECH_STATE_FILE"):
            stateFile = os.environ["CONTECH_STATE_FILE"]
        else:
            stateFile = CONTECH_HOME + "/scripts/output/contechStateFile.temp"

    else:        
        print ">Error: Could not find contech installation. Set CONTECH_HOME to the root of your contech directory."
        exit(1)
    
    #LOCAL = "/net/tinker/local"
    #LLVMCONTECH = LOCAL + "/lib/LLVMContech.so"
    #RUNTIME = LOCAL + "/lib/libct_runtime.a"
    
    
    # Name of the .c file to be processed
    cfile="" 
    # Names of the .o files to be linked
    ofiles=""
    # .o files hiding in .a files to be linked
    oFromAFiles=""
    # Name of the output file
    out=""
    # All remaining flags to be passed on to clang
    if ARM == True:
        CFLAGS="--verbose -pthread"
    else:
        CFLAGS="-flto --verbose -pthread"

    # Choose correct compiler
    if isCpp:
        CC = CLANGPP
    else:
        CC = CLANG
    
    outFileComingNext = False;
    compileOnly = False;
    depsOnly = False;
    dragon = False;
    MPI = False;
    
    for arg in sys.argv[1:]:

        # This compile step is just generating dependencies, don't compile or link unless instructed to
        if "-M" in arg:
            depsOnly = True
    
        # -o, look for filename next
        elif "-o" == arg:
            outFileComingNext = True
        
        # -o attached to other options
        elif "-o," in arg and out == "":
            
            # Option might have been passed to the linker with something like -Wl,-o,out
            if "," in arg:
                for token in arg.split(","):
                    if outFileComingNext == True:
                        out = token
                        outFileComingNext = False
                    elif token == "-o":
                        outFileComingNext = True
                # Make sure the whole option makes it to the CFLAGS, just in case
                CFLAGS = CFLAGS + " " + arg
                
            # -o attached to the front of the name
            else:
                out = arg
                out = out.replace("-o","",1)
            # TODO What other horrible ways are there to pass the name of the output file???
        
        # Saw -o at last arg
        elif outFileComingNext:
            out = arg
            outFileComingNext = False
            
        # C++ file
        elif ".cxx" == arg[-4:]:
            cfile = arg
            isCpp = True
        elif ".cpp" == arg[-4:]:
            cfile = arg
            isCpp = True
        elif ".cc" == arg[-3:]:
            cfile = arg
            isCpp = True
        # C file
        elif ".c" == arg[-2:]:
            cfile = arg
            isCpp = False
        elif ".f" == arg[-2:]:
            # Fortran only supported through dragonEgg and not clang
            cfile = arg
            dragon = True
        # Object file
        elif ".o" == arg[-2:]:
            ofiles = ofiles + " " + arg
        
        # Compile only
        elif "-c" == arg:
            compileOnly = True
        elif "-lmpi" == arg[0:5]:
            MPI = True
            CFLAGS = CFLAGS + " " + arg

        # Combine other args into CFLAGS
        else:
            CFLAGS = CFLAGS + " " + arg


            
    # Debug
    #print ""
    #print "Contech wrapper: "
    #print "cfile=" + cfile
    #print "ofiles=" + ofiles
    #print "CFLAGS=" + CFLAGS
    #print "out=" + outdo

    # Found some flag that we don't handle, just pass through the compiler and exit
    if depsOnly and not compileOnly:
        passThrough(CC)
    
    # Compile requested but no input found, let the compiler throw an error
    if cfile == "" and compileOnly:
        passThrough(CC)
    
    # Requires dragon egg
    elif dragon == True:
        name=cfile[0: len(cfile) - 2]
        
        # Define names of intermediate files
        A= name + ".ll"
        B= name + "_ct.bc"
        
        # Define name of compiled file
        newobj = ""
        if out != "" :
            newobj = out
        else:
            newobj = name + ".o"
        
        # Make sure the output ends in .o
        if newobj[-2:] != ".o":
            newobj = newobj + ".o"
        
        # -fplugin=$CONTECH_LLVM_HOME/lib64/dragonegg.so -S -fplugin-arg-dragonegg-emit-ir
        pcall(["gcc", CFLAGS, "-S", "-fplugin=" + CONTECH_LLVM_HOME + "/lib/dragonegg.so" , "-fplugin-arg-dragonegg-emit-ir", cfile, "-o", A])
        
        pcall([OPT, "-load=" + LLVMCONTECH, "-Contech", A, "-o", B, "-ContechState", stateFile])
        # Compile bitcode back to a .o file
        pcall([CC, CFLAGS, "-c", "-o", newobj, B])
        # Add the generated object file to the list of things to link
        ofiles = ofiles + " " + newobj
        
    # Input file found, assuming compile requested.
    # Compile with contech   
    elif cfile != "":
        
        # Get the name of cfile without an extension
        if isCpp:
            name=cfile.replace(".cpp","",1);
            name=name.replace(".cc","",1);
        else:
        #linux.contech ... 
            name=cfile[0: len(cfile) - 2]
        
        # Define names of intermediate files
        A= name + ".bc"
        B= name + "_ct.bc"

        # Define name of compiled file
        newobj = ""
        if out != "" :
            newobj = out
        else:
            newobj = name + ".o"
        
        # Make sure the output ends in .o
        if newobj[-2:] != ".o":
            newobj = newobj + ".o"

        # Compile with clang to emit LLVM bitcode
        pcall([CC, CFLAGS, cfile, "-emit-llvm", "-c", "-o", A])
        # Run the Contech pass to add instrumentation
        
        if markOnly:
            pcall([OPT, "-load=" + LLVMCONTECH, "-Contech", A, "-o", B, "-ContechState", stateFile, "-ContechMarkFE"])
        elif minimal:
            pcall([OPT, "-load=" + LLVMCONTECH, "-Contech", A, "-o", B, "-ContechState", stateFile, "-ContechMinimal"])
        elif hammer:
            hammerNailFile = os.environ["HAMMER_NAIL_FILE"]
            hammerOptLevel = os.environ["HAMMER_OPT_LEVEL"]
            pcall([OPT, "-load=" + LLVMHAMMER, "-Hammer", A, "-o", B, "-HammerState", stateFile, "-HammerNailFile", hammerNailFile, "-HammerOptLevel", hammerOptLevel])
        else:
            if ARM == True:
                pcall([OPT, "-load=" + LLVMCONTECH, "-Contech", A, "-o", newobj, "-ContechState", stateFile])
            else:
                pcall([OPT, "-load=" + LLVMCONTECH, "-Contech", A, "-o", B, "-ContechState", stateFile])
        # Compile bitcode back to a .o file
        if ARM == True:
            print ""
        else:
            pcall([CC, CFLAGS, "-c", "-o", newobj, B])
        # Add the generated object file to the list of things to link
        ofiles = ofiles + " " + newobj
        
    # Link 
    if not compileOnly:
        if ofiles != "":
            
            # Define name of final executable
            if out == "":
                out = "a.out"
                
            if hammer:
                # Compile final executable
                pcall([CC, ofiles, CFLAGS, "-o", out, "-flto", "-lpthread", "-lz"])
            else:
                # Link in basic block table
                shutil.copyfile(stateFile, "contech.bin")
                # Note that we may have to create two .o, one for 32bit and one for 64bit
                OBJCOPY = "objcopy"
                
                if ARM == True:
                    pcall([OBJCOPY, "--input binary", "--output elf32-littlearm", "--binary-architecture arm", "contech.bin", "contech_state.o"])
                else:
                    pcall([OBJCOPY, "--input binary", "--output elf64-x86-64", "--binary-architecture i386", "contech.bin", "contech_state.o"])
                    #pcall([OBJCOPY, "--input binary", "--output elf32-i386", "--binary-architecture i386", "contech.bin", "contech_state.o"])

                # Does the binary use MPI?
                if MPI == True:
                    RUNTIME = RUNTIME + CONTECH_HOME + "/common/runtime/ct_mpi.bc "
                else:
                    RUNTIME = RUNTIME + CONTECH_HOME + "/common/runtime/ct_nompi.bc "
                    
                # Compile final executable

                # But first extract .o files hiding in .a libraries
                TMPARDIR = tempfile.mkdtemp()
                if os.path.isdir(TMPARDIR):
                    pcall(["mkdir",  "-p", TMPARDIR])

                    for flag in CFLAGS.split():
                        if ".a" == flag[-2:]:
                            aFilename = flag
                            if not flag.find("/") == -1:
                                aFilename = flag.split("/")[-1]
                            pcall(["cp", flag, TMPARDIR])
                            pcall(["cd", TMPARDIR, ";", CONTECH_HOME + "/scripts/uniq_ar_x.sh", aFilename])
                    oFromA = glob.glob(TMPARDIR + "/*.o")
                    for f in oFromA:
                        head = subprocess.Popen(["head", "-c", "3", f], stdout=subprocess.PIPE)
                        oFromAType = head.stdout.read()
                        if oFromAType.startswith("BC"):
                            oFromAFiles = oFromAFiles + " " + f 

                    pcall(["llvm-link", ofiles, RUNTIME, oFromAFiles, "-o", out + "_ct.link.bc"])
                    pcall(["rm",  "-rf", TMPARDIR])

                if ARM == True:
                    pcall([OPT, "-always-inline", out + "_ct.link.bc", "-o", out + "_ct_inline.bc"])
                    pcall([CC, CFLAGS, "-c -o", out + "_ct.o", out + "_ct_inline.bc"])
                    pcall([CC, out + "_ct.o", CFLAGS, "-o", out, "-lpthread", "contech_state.o"])
                else:
                    pcall([CC, RUNTIME, ofiles, CFLAGS, "-o", out, "-lrt", "-flto", "-lpthread", "contech_state.o"])
                        
        else:
            passThrough(CC)
Beispiel #15
0
def main():
    parser = argparse.ArgumentParser(
        description="Builds a parsec benchmark with contech")
    parser.add_argument("benchmark", help="The parsec bencmark to build.")
    parser.add_argument(
        "--bldconf",
        default="contech",
        help="PARSEC build configuration (bldconf) to use for building.")
    parser.add_argument(
        "--hammerOptLevel",
        help="Set hammer optimization level (bldconf==hammer only)")
    args = parser.parse_args()

    if os.environ.has_key("CONTECH_HOME"):
        CONTECH_HOME = os.environ["CONTECH_HOME"]
        stateFile = os.path.join(CONTECH_HOME, "scripts/output/",
                                 args.benchmark + ".stateFile.temp")
        os.environ["CONTECH_STATE_FILE"] = stateFile
    else:
        print ">Error: Could not find contech installation. Set CONTECH_HOME to the root of your contech directory."
        exit(1)

    if os.environ.has_key("PARSEC_HOME"):
        PARSEC_HOME = os.environ["PARSEC_HOME"]
        PARSECMGMT = os.path.join(PARSEC_HOME, "bin/parsecmgmt")
    else:
        print ">Error: Could not find parsec installation. Set PARSEC_HOME to the root of your parsec directory."
        exit(1)

    # Run the parsec benchmark
    print ">Building " + args.benchmark

    # Prepare state file for run
    if os.path.exists(stateFile):
        os.remove(stateFile)

    # Hammer: Prepare hammer nail file
    if args.bldconf == "hammer":
        os.environ["HAMMER_NAIL_FILE"] = os.path.join(
            CONTECH_HOME,
            "backend/Hammer/compilerData/{}.bin".format(args.benchmark))
        os.environ["HAMMER_OPT_LEVEL"] = args.hammerOptLevel

    pcall([
        PARSECMGMT, "-a", "uninstall", "-p", args.benchmark, "-c", args.bldconf
    ])
    with Timer("Build"):
        returnCode = pcall([
            PARSECMGMT, "-a", "build", "-p", args.benchmark, "-c", args.bldconf
        ],
                           returnCode=True)

    # Clean up
    if os.path.exists(stateFile):
        os.remove(stateFile)
    elif args.bldconf in ["contech", "contech_marker", "hammer"]:
        util.print_warning("Warning: no statefile was generated.")

    if returnCode != 0:
        util.print_error("Build failed")
        exit(1)
def passThrough(CC):
    #print("Oh, no.")
    command = [CC] + sys.argv[1:]
    pcall(command, silent=True)
    exit(0)