10eae32dcSDimitry Andric //===--- HIPUtility.cpp - Common HIP Tool Chain Utilities -------*- C++ -*-===// 20eae32dcSDimitry Andric // 30eae32dcSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40eae32dcSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60eae32dcSDimitry Andric // 70eae32dcSDimitry Andric //===----------------------------------------------------------------------===// 80eae32dcSDimitry Andric 90eae32dcSDimitry Andric #include "HIPUtility.h" 100eae32dcSDimitry Andric #include "CommonArgs.h" 110eae32dcSDimitry Andric #include "clang/Driver/Compilation.h" 120eae32dcSDimitry Andric #include "llvm/ADT/StringRef.h" 130eae32dcSDimitry Andric #include "llvm/Support/Path.h" 14*06c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h" 150eae32dcSDimitry Andric 160eae32dcSDimitry Andric using namespace clang::driver; 170eae32dcSDimitry Andric using namespace clang::driver::tools; 180eae32dcSDimitry Andric using namespace llvm::opt; 190eae32dcSDimitry Andric 200eae32dcSDimitry Andric #if defined(_WIN32) || defined(_WIN64) 210eae32dcSDimitry Andric #define NULL_FILE "nul" 220eae32dcSDimitry Andric #else 230eae32dcSDimitry Andric #define NULL_FILE "/dev/null" 240eae32dcSDimitry Andric #endif 250eae32dcSDimitry Andric 260eae32dcSDimitry Andric namespace { 270eae32dcSDimitry Andric const unsigned HIPCodeObjectAlign = 4096; 280eae32dcSDimitry Andric } // namespace 290eae32dcSDimitry Andric 300eae32dcSDimitry Andric // Constructs a triple string for clang offload bundler. 310eae32dcSDimitry Andric static std::string normalizeForBundler(const llvm::Triple &T, 320eae32dcSDimitry Andric bool HasTargetID) { 330eae32dcSDimitry Andric return HasTargetID ? (T.getArchName() + "-" + T.getVendorName() + "-" + 340eae32dcSDimitry Andric T.getOSName() + "-" + T.getEnvironmentName()) 350eae32dcSDimitry Andric .str() 360eae32dcSDimitry Andric : T.normalize(); 370eae32dcSDimitry Andric } 380eae32dcSDimitry Andric 390eae32dcSDimitry Andric // Construct a clang-offload-bundler command to bundle code objects for 400eae32dcSDimitry Andric // different devices into a HIP fat binary. 410eae32dcSDimitry Andric void HIP::constructHIPFatbinCommand(Compilation &C, const JobAction &JA, 420eae32dcSDimitry Andric llvm::StringRef OutputFileName, 430eae32dcSDimitry Andric const InputInfoList &Inputs, 440eae32dcSDimitry Andric const llvm::opt::ArgList &Args, 450eae32dcSDimitry Andric const Tool &T) { 460eae32dcSDimitry Andric // Construct clang-offload-bundler command to bundle object files for 470eae32dcSDimitry Andric // for different GPU archs. 480eae32dcSDimitry Andric ArgStringList BundlerArgs; 490eae32dcSDimitry Andric BundlerArgs.push_back(Args.MakeArgString("-type=o")); 500eae32dcSDimitry Andric BundlerArgs.push_back( 510eae32dcSDimitry Andric Args.MakeArgString("-bundle-align=" + Twine(HIPCodeObjectAlign))); 520eae32dcSDimitry Andric 530eae32dcSDimitry Andric // ToDo: Remove the dummy host binary entry which is required by 540eae32dcSDimitry Andric // clang-offload-bundler. 550eae32dcSDimitry Andric std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux"; 560eae32dcSDimitry Andric // AMDGCN: 570eae32dcSDimitry Andric // For code object version 2 and 3, the offload kind in bundle ID is 'hip' 580eae32dcSDimitry Andric // for backward compatibility. For code object version 4 and greater, the 590eae32dcSDimitry Andric // offload kind in bundle ID is 'hipv4'. 600eae32dcSDimitry Andric std::string OffloadKind = "hip"; 610eae32dcSDimitry Andric auto &TT = T.getToolChain().getTriple(); 620eae32dcSDimitry Andric if (TT.isAMDGCN() && getAMDGPUCodeObjectVersion(C.getDriver(), Args) >= 4) 630eae32dcSDimitry Andric OffloadKind = OffloadKind + "v4"; 640eae32dcSDimitry Andric for (const auto &II : Inputs) { 650eae32dcSDimitry Andric const auto *A = II.getAction(); 660eae32dcSDimitry Andric auto ArchStr = llvm::StringRef(A->getOffloadingArch()); 670eae32dcSDimitry Andric BundlerTargetArg += 680eae32dcSDimitry Andric "," + OffloadKind + "-" + normalizeForBundler(TT, !ArchStr.empty()); 690eae32dcSDimitry Andric if (!ArchStr.empty()) 700eae32dcSDimitry Andric BundlerTargetArg += "-" + ArchStr.str(); 710eae32dcSDimitry Andric } 720eae32dcSDimitry Andric BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg)); 7381ad6265SDimitry Andric 7481ad6265SDimitry Andric // Use a NULL file as input for the dummy host binary entry 7581ad6265SDimitry Andric std::string BundlerInputArg = "-input=" NULL_FILE; 760eae32dcSDimitry Andric BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg)); 7781ad6265SDimitry Andric for (const auto &II : Inputs) { 7881ad6265SDimitry Andric BundlerInputArg = std::string("-input=") + II.getFilename(); 7981ad6265SDimitry Andric BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg)); 8081ad6265SDimitry Andric } 810eae32dcSDimitry Andric 820eae32dcSDimitry Andric std::string Output = std::string(OutputFileName); 830eae32dcSDimitry Andric auto *BundlerOutputArg = 8481ad6265SDimitry Andric Args.MakeArgString(std::string("-output=").append(Output)); 850eae32dcSDimitry Andric BundlerArgs.push_back(BundlerOutputArg); 860eae32dcSDimitry Andric 870eae32dcSDimitry Andric const char *Bundler = Args.MakeArgString( 880eae32dcSDimitry Andric T.getToolChain().GetProgramPath("clang-offload-bundler")); 890eae32dcSDimitry Andric C.addCommand(std::make_unique<Command>( 900eae32dcSDimitry Andric JA, T, ResponseFileSupport::None(), Bundler, BundlerArgs, Inputs, 910eae32dcSDimitry Andric InputInfo(&JA, Args.MakeArgString(Output)))); 920eae32dcSDimitry Andric } 930eae32dcSDimitry Andric 940eae32dcSDimitry Andric /// Add Generated HIP Object File which has device images embedded into the 950eae32dcSDimitry Andric /// host to the argument list for linking. Using MC directives, embed the 960eae32dcSDimitry Andric /// device code and also define symbols required by the code generation so that 970eae32dcSDimitry Andric /// the image can be retrieved at runtime. 980eae32dcSDimitry Andric void HIP::constructGenerateObjFileFromHIPFatBinary( 990eae32dcSDimitry Andric Compilation &C, const InputInfo &Output, const InputInfoList &Inputs, 1000eae32dcSDimitry Andric const ArgList &Args, const JobAction &JA, const Tool &T) { 1010eae32dcSDimitry Andric const ToolChain &TC = T.getToolChain(); 1020eae32dcSDimitry Andric std::string Name = std::string(llvm::sys::path::stem(Output.getFilename())); 1030eae32dcSDimitry Andric 1040eae32dcSDimitry Andric // Create Temp Object File Generator, 1050eae32dcSDimitry Andric // Offload Bundled file and Bundled Object file. 1060eae32dcSDimitry Andric // Keep them if save-temps is enabled. 1070eae32dcSDimitry Andric const char *McinFile; 1080eae32dcSDimitry Andric const char *BundleFile; 1090eae32dcSDimitry Andric if (C.getDriver().isSaveTempsEnabled()) { 1100eae32dcSDimitry Andric McinFile = C.getArgs().MakeArgString(Name + ".mcin"); 1110eae32dcSDimitry Andric BundleFile = C.getArgs().MakeArgString(Name + ".hipfb"); 1120eae32dcSDimitry Andric } else { 1130eae32dcSDimitry Andric auto TmpNameMcin = C.getDriver().GetTemporaryPath(Name, "mcin"); 1140eae32dcSDimitry Andric McinFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameMcin)); 1150eae32dcSDimitry Andric auto TmpNameFb = C.getDriver().GetTemporaryPath(Name, "hipfb"); 1160eae32dcSDimitry Andric BundleFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameFb)); 1170eae32dcSDimitry Andric } 1180eae32dcSDimitry Andric HIP::constructHIPFatbinCommand(C, JA, BundleFile, Inputs, Args, T); 1190eae32dcSDimitry Andric 1200eae32dcSDimitry Andric // Create a buffer to write the contents of the temp obj generator. 1210eae32dcSDimitry Andric std::string ObjBuffer; 1220eae32dcSDimitry Andric llvm::raw_string_ostream ObjStream(ObjBuffer); 1230eae32dcSDimitry Andric 1240eae32dcSDimitry Andric auto HostTriple = 1250eae32dcSDimitry Andric C.getSingleOffloadToolChain<Action::OFK_Host>()->getTriple(); 1260eae32dcSDimitry Andric 1270eae32dcSDimitry Andric // Add MC directives to embed target binaries. We ensure that each 1280eae32dcSDimitry Andric // section and image is 16-byte aligned. This is not mandatory, but 1290eae32dcSDimitry Andric // increases the likelihood of data to be aligned with a cache block 1300eae32dcSDimitry Andric // in several main host machines. 1310eae32dcSDimitry Andric ObjStream << "# HIP Object Generator\n"; 1320eae32dcSDimitry Andric ObjStream << "# *** Automatically generated by Clang ***\n"; 1330eae32dcSDimitry Andric if (HostTriple.isWindowsMSVCEnvironment()) { 1340eae32dcSDimitry Andric ObjStream << " .section .hip_fatbin, \"dw\"\n"; 1350eae32dcSDimitry Andric } else { 1360eae32dcSDimitry Andric ObjStream << " .protected __hip_fatbin\n"; 1370eae32dcSDimitry Andric ObjStream << " .type __hip_fatbin,@object\n"; 1380eae32dcSDimitry Andric ObjStream << " .section .hip_fatbin,\"a\",@progbits\n"; 1390eae32dcSDimitry Andric } 1400eae32dcSDimitry Andric ObjStream << " .globl __hip_fatbin\n"; 1410eae32dcSDimitry Andric ObjStream << " .p2align " << llvm::Log2(llvm::Align(HIPCodeObjectAlign)) 1420eae32dcSDimitry Andric << "\n"; 1430eae32dcSDimitry Andric ObjStream << "__hip_fatbin:\n"; 1440eae32dcSDimitry Andric ObjStream << " .incbin "; 1450eae32dcSDimitry Andric llvm::sys::printArg(ObjStream, BundleFile, /*Quote=*/true); 1460eae32dcSDimitry Andric ObjStream << "\n"; 1470eae32dcSDimitry Andric ObjStream.flush(); 1480eae32dcSDimitry Andric 1490eae32dcSDimitry Andric // Dump the contents of the temp object file gen if the user requested that. 1500eae32dcSDimitry Andric // We support this option to enable testing of behavior with -###. 1510eae32dcSDimitry Andric if (C.getArgs().hasArg(options::OPT_fhip_dump_offload_linker_script)) 1520eae32dcSDimitry Andric llvm::errs() << ObjBuffer; 1530eae32dcSDimitry Andric 1540eae32dcSDimitry Andric // Open script file and write the contents. 1550eae32dcSDimitry Andric std::error_code EC; 1560eae32dcSDimitry Andric llvm::raw_fd_ostream Objf(McinFile, EC, llvm::sys::fs::OF_None); 1570eae32dcSDimitry Andric 1580eae32dcSDimitry Andric if (EC) { 1590eae32dcSDimitry Andric C.getDriver().Diag(clang::diag::err_unable_to_make_temp) << EC.message(); 1600eae32dcSDimitry Andric return; 1610eae32dcSDimitry Andric } 1620eae32dcSDimitry Andric 1630eae32dcSDimitry Andric Objf << ObjBuffer; 1640eae32dcSDimitry Andric 1650eae32dcSDimitry Andric ArgStringList McArgs{"-triple", Args.MakeArgString(HostTriple.normalize()), 1660eae32dcSDimitry Andric "-o", Output.getFilename(), 1670eae32dcSDimitry Andric McinFile, "--filetype=obj"}; 1680eae32dcSDimitry Andric const char *Mc = Args.MakeArgString(TC.GetProgramPath("llvm-mc")); 1690eae32dcSDimitry Andric C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(), Mc, 1700eae32dcSDimitry Andric McArgs, Inputs, Output)); 1710eae32dcSDimitry Andric } 172