mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2024-11-26 15:11:00 +00:00
[CUDA][HIP] Add -fuse-cuid
This patch added a distinct CUID for each input file, which is represented by InputAction. clang initially creates an InputAction for each input file for the host compilation. In CUDA/HIP action builder, each InputAction is given a CUID and cloned for each GPU arch, and the CUID is also cloned. In this way, we guarantee the corresponding device and host compilation for the same file shared the same CUID. On the other hand, different compilation units have different CUID. -fuse-cuid=random|hash|none is added to control the method to generate CUID. The default is hash. -cuid=X is also added to specify CUID explicitly, which overrides -fuse-cuid. Reviewed by: Artem Belevich Differential Revision: https://reviews.llvm.org/D95007
This commit is contained in:
parent
7dc324aafa
commit
98c21289f1
@ -331,6 +331,12 @@ public:
|
||||
/// host code generation.
|
||||
std::string OMPHostIRFile;
|
||||
|
||||
/// The user provided compilation unit ID, if non-empty. This is used to
|
||||
/// externalize static variables which is needed to support accessing static
|
||||
/// device variables in host code for single source offloading languages
|
||||
/// like CUDA/HIP.
|
||||
std::string CUID;
|
||||
|
||||
/// Indicates whether the front-end is explicitly told that the
|
||||
/// input is a header file (i.e. -x c-header).
|
||||
bool IsHeaderFile = false;
|
||||
|
@ -214,14 +214,18 @@ public:
|
||||
|
||||
class InputAction : public Action {
|
||||
const llvm::opt::Arg &Input;
|
||||
|
||||
std::string Id;
|
||||
virtual void anchor();
|
||||
|
||||
public:
|
||||
InputAction(const llvm::opt::Arg &Input, types::ID Type);
|
||||
InputAction(const llvm::opt::Arg &Input, types::ID Type,
|
||||
StringRef Id = StringRef());
|
||||
|
||||
const llvm::opt::Arg &getInputArg() const { return Input; }
|
||||
|
||||
void setId(StringRef _Id) { Id = _Id.str(); }
|
||||
StringRef getId() const { return Id; }
|
||||
|
||||
static bool classof(const Action *A) {
|
||||
return A->getKind() == InputClass;
|
||||
}
|
||||
|
@ -930,6 +930,18 @@ def gpu_max_threads_per_block_EQ : Joined<["--"], "gpu-max-threads-per-block=">,
|
||||
def gpu_instrument_lib_EQ : Joined<["--"], "gpu-instrument-lib=">,
|
||||
HelpText<"Instrument device library for HIP, which is a LLVM bitcode containing "
|
||||
"__cyg_profile_func_enter and __cyg_profile_func_exit">;
|
||||
def cuid_EQ : Joined<["-"], "cuid=">, Flags<[CC1Option]>,
|
||||
HelpText<"An ID for compilation unit, which should be the same for the same "
|
||||
"compilation unit but different for different compilation units. "
|
||||
"It is used to externalize device-side static variables for single "
|
||||
"source offloading languages CUDA and HIP so that they can be "
|
||||
"accessed by the host code of the same compilation unit.">;
|
||||
def fuse_cuid_EQ : Joined<["-"], "fuse-cuid=">,
|
||||
HelpText<"Method to generate ID's for compilation units for single source "
|
||||
"offloading languages CUDA and HIP: 'hash' (ID's generated by hashing "
|
||||
"file path and command line options) | 'random' (ID's generated as "
|
||||
"random numbers) | 'none' (disabled). Default is 'hash'. This option "
|
||||
"will be overriden by option '-cuid=[ID]' if it is specified." >;
|
||||
def libomptarget_nvptx_bc_path_EQ : Joined<["--"], "libomptarget-nvptx-bc-path=">, Group<i_Group>,
|
||||
HelpText<"Path to libomptarget-nvptx bitcode library">;
|
||||
def dD : Flag<["-"], "dD">, Group<d_Group>, Flags<[CC1Option]>,
|
||||
|
@ -165,8 +165,8 @@ StringRef Action::GetOffloadKindName(OffloadKind Kind) {
|
||||
|
||||
void InputAction::anchor() {}
|
||||
|
||||
InputAction::InputAction(const Arg &_Input, types::ID _Type)
|
||||
: Action(InputClass, _Type), Input(_Input) {}
|
||||
InputAction::InputAction(const Arg &_Input, types::ID _Type, StringRef _Id)
|
||||
: Action(InputClass, _Type), Input(_Input), Id(_Id.str()) {}
|
||||
|
||||
void BindArchAction::anchor() {}
|
||||
|
||||
|
@ -77,6 +77,7 @@
|
||||
#include "llvm/Support/FileSystem.h"
|
||||
#include "llvm/Support/FormatVariadic.h"
|
||||
#include "llvm/Support/Host.h"
|
||||
#include "llvm/Support/MD5.h"
|
||||
#include "llvm/Support/Path.h"
|
||||
#include "llvm/Support/PrettyStackTrace.h"
|
||||
#include "llvm/Support/Process.h"
|
||||
@ -2453,6 +2454,14 @@ class OffloadingActionBuilder final {
|
||||
/// Default GPU architecture if there's no one specified.
|
||||
CudaArch DefaultCudaArch = CudaArch::UNKNOWN;
|
||||
|
||||
/// Method to generate compilation unit ID specified by option
|
||||
/// '-fuse-cuid='.
|
||||
enum UseCUIDKind { CUID_Hash, CUID_Random, CUID_None, CUID_Invalid };
|
||||
UseCUIDKind UseCUID = CUID_Hash;
|
||||
|
||||
/// Compilation unit ID specified by option '-cuid='.
|
||||
StringRef FixedCUID;
|
||||
|
||||
public:
|
||||
CudaActionBuilderBase(Compilation &C, DerivedArgList &Args,
|
||||
const Driver::InputList &Inputs,
|
||||
@ -2489,9 +2498,32 @@ class OffloadingActionBuilder final {
|
||||
// Replicate inputs for each GPU architecture.
|
||||
auto Ty = IA->getType() == types::TY_HIP ? types::TY_HIP_DEVICE
|
||||
: types::TY_CUDA_DEVICE;
|
||||
std::string CUID = FixedCUID.str();
|
||||
if (CUID.empty()) {
|
||||
if (UseCUID == CUID_Random)
|
||||
CUID = llvm::utohexstr(llvm::sys::Process::GetRandomNumber(),
|
||||
/*LowerCase=*/true);
|
||||
else if (UseCUID == CUID_Hash) {
|
||||
llvm::MD5 Hasher;
|
||||
llvm::MD5::MD5Result Hash;
|
||||
SmallString<256> RealPath;
|
||||
llvm::sys::fs::real_path(IA->getInputArg().getValue(), RealPath,
|
||||
/*expand_tilde=*/true);
|
||||
Hasher.update(RealPath);
|
||||
for (auto *A : Args) {
|
||||
if (A->getOption().matches(options::OPT_INPUT))
|
||||
continue;
|
||||
Hasher.update(A->getAsString(Args));
|
||||
}
|
||||
Hasher.final(Hash);
|
||||
CUID = llvm::utohexstr(Hash.low(), /*LowerCase=*/true);
|
||||
}
|
||||
}
|
||||
IA->setId(CUID);
|
||||
|
||||
for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
|
||||
CudaDeviceActions.push_back(
|
||||
C.MakeAction<InputAction>(IA->getInputArg(), Ty));
|
||||
C.MakeAction<InputAction>(IA->getInputArg(), Ty, IA->getId()));
|
||||
}
|
||||
|
||||
return ABRT_Success;
|
||||
@ -2613,6 +2645,21 @@ class OffloadingActionBuilder final {
|
||||
options::OPT_cuda_device_only);
|
||||
EmitLLVM = Args.getLastArg(options::OPT_emit_llvm);
|
||||
EmitAsm = Args.getLastArg(options::OPT_S);
|
||||
FixedCUID = Args.getLastArgValue(options::OPT_cuid_EQ);
|
||||
if (Arg *A = Args.getLastArg(options::OPT_fuse_cuid_EQ)) {
|
||||
StringRef UseCUIDStr = A->getValue();
|
||||
UseCUID = llvm::StringSwitch<UseCUIDKind>(UseCUIDStr)
|
||||
.Case("hash", CUID_Hash)
|
||||
.Case("random", CUID_Random)
|
||||
.Case("none", CUID_None)
|
||||
.Default(CUID_Invalid);
|
||||
if (UseCUID == CUID_Invalid) {
|
||||
C.getDriver().Diag(diag::err_drv_invalid_value)
|
||||
<< A->getAsString(Args) << UseCUIDStr;
|
||||
C.setContainsError();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Collect all cuda_gpu_arch parameters, removing duplicates.
|
||||
std::set<StringRef> GpuArchs;
|
||||
|
@ -6332,6 +6332,18 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
CmdArgs.push_back("-fcuda-short-ptr");
|
||||
}
|
||||
|
||||
if (IsCuda || IsHIP) {
|
||||
// Determine the original source input.
|
||||
const Action *SourceAction = &JA;
|
||||
while (SourceAction->getKind() != Action::InputClass) {
|
||||
assert(!SourceAction->getInputs().empty() && "unexpected root action!");
|
||||
SourceAction = SourceAction->getInputs()[0];
|
||||
}
|
||||
auto CUID = cast<InputAction>(SourceAction)->getId();
|
||||
if (!CUID.empty())
|
||||
CmdArgs.push_back(Args.MakeArgString(Twine("-cuid=") + Twine(CUID)));
|
||||
}
|
||||
|
||||
if (IsHIP)
|
||||
CmdArgs.push_back("-fcuda-allow-variadic-functions");
|
||||
|
||||
|
@ -2741,6 +2741,11 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (auto *A = Args.getLastArg(OPT_cuid_EQ)) {
|
||||
Opts.CUID = std::string(A->getValue());
|
||||
}
|
||||
|
||||
if (Opts.ObjC) {
|
||||
if (Arg *arg = Args.getLastArg(OPT_fobjc_runtime_EQ)) {
|
||||
StringRef value = arg->getValue();
|
||||
|
35
clang/test/Driver/hip-cuid-hash.hip
Normal file
35
clang/test/Driver/hip-cuid-hash.hip
Normal file
@ -0,0 +1,35 @@
|
||||
// REQUIRES: clang-driver
|
||||
// REQUIRES: x86-registered-target
|
||||
// REQUIRES: amdgpu-registered-target
|
||||
|
||||
// Check CUID generated by hash.
|
||||
// The same CUID is generated for the same file with the same options.
|
||||
|
||||
// RUN: %clang -### -x hip -target x86_64-unknown-linux-gnu \
|
||||
// RUN: --offload-arch=gfx906 -c -nogpulib -fuse-cuid=hash \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/a.cu >%t.out 2>&1
|
||||
|
||||
// RUN: %clang -### -x hip -target x86_64-unknown-linux-gnu \
|
||||
// RUN: --offload-arch=gfx906 -c -nogpulib -fuse-cuid=hash \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/a.cu >>%t.out 2>&1
|
||||
|
||||
// RUN: FileCheck %s -check-prefixes=SAME -input-file %t.out
|
||||
|
||||
// Check CUID generated by hash.
|
||||
// Different CUID's are generated for the same file with different options.
|
||||
|
||||
// RUN: %clang -### -x hip -target x86_64-unknown-linux-gnu -DX=1 \
|
||||
// RUN: --offload-arch=gfx906 -c -nogpulib -fuse-cuid=hash \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/a.cu >%t.out 2>&1
|
||||
|
||||
// RUN: %clang -### -x hip -target x86_64-unknown-linux-gnu -DX=2 \
|
||||
// RUN: --offload-arch=gfx906 -c -nogpulib -fuse-cuid=hash \
|
||||
// RUN: %S/Inputs/../Inputs/hip_multiple_inputs/a.cu >>%t.out 2>&1
|
||||
|
||||
// RUN: FileCheck %s -check-prefixes=DIFF -input-file %t.out
|
||||
|
||||
// SAME: "{{.*}}clang{{.*}}" {{.*}} "-target-cpu" "gfx906" {{.*}}"-cuid=[[CUID:[0-9a-f]+]]"
|
||||
// SAME: "{{.*}}clang{{.*}}" {{.*}} "-target-cpu" "gfx906" {{.*}}"-cuid=[[CUID]]"
|
||||
|
||||
// DIFF: "{{.*}}clang{{.*}}" {{.*}} "-target-cpu" "gfx906" {{.*}}"-cuid=[[CUID:[0-9a-f]+]]"
|
||||
// DIFF-NOT: "{{.*}}clang{{.*}}" {{.*}} "-target-cpu" "gfx906" {{.*}}"-cuid=[[CUID]]"
|
93
clang/test/Driver/hip-cuid.hip
Normal file
93
clang/test/Driver/hip-cuid.hip
Normal file
@ -0,0 +1,93 @@
|
||||
// REQUIRES: clang-driver
|
||||
// REQUIRES: x86-registered-target
|
||||
// REQUIRES: amdgpu-registered-target
|
||||
|
||||
// Check invalid -fuse-cuid= option.
|
||||
|
||||
// RUN: not %clang -### -x hip \
|
||||
// RUN: -target x86_64-unknown-linux-gnu \
|
||||
// RUN: --offload-arch=gfx900 \
|
||||
// RUN: --offload-arch=gfx906 \
|
||||
// RUN: -c -nogpulib -fuse-cuid=invalid \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/b.hip \
|
||||
// RUN: 2>&1 | FileCheck -check-prefixes=INVALID %s
|
||||
|
||||
// Check random CUID generator.
|
||||
|
||||
// RUN: %clang -### -x hip \
|
||||
// RUN: -target x86_64-unknown-linux-gnu \
|
||||
// RUN: --offload-arch=gfx900 \
|
||||
// RUN: --offload-arch=gfx906 \
|
||||
// RUN: -c -nogpulib -fuse-cuid=random \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/b.hip \
|
||||
// RUN: 2>&1 | FileCheck -check-prefixes=COMMON,HEX %s
|
||||
|
||||
// Check fixed CUID.
|
||||
|
||||
// RUN: %clang -### -x hip \
|
||||
// RUN: -target x86_64-unknown-linux-gnu \
|
||||
// RUN: --offload-arch=gfx900 \
|
||||
// RUN: --offload-arch=gfx906 \
|
||||
// RUN: -c -nogpulib -cuid=xyz_123 \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/b.hip \
|
||||
// RUN: 2>&1 | FileCheck -check-prefixes=COMMON,FIXED %s
|
||||
|
||||
// Check fixed CUID override -fuse-cuid.
|
||||
|
||||
// RUN: %clang -### -x hip \
|
||||
// RUN: -target x86_64-unknown-linux-gnu \
|
||||
// RUN: --offload-arch=gfx900 \
|
||||
// RUN: --offload-arch=gfx906 \
|
||||
// RUN: -c -nogpulib -fuse-cuid=random -cuid=xyz_123 \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/b.hip \
|
||||
// RUN: 2>&1 | FileCheck -check-prefixes=COMMON,FIXED %s
|
||||
|
||||
// Check hash CUID generator.
|
||||
|
||||
// RUN: %clang -### -x hip \
|
||||
// RUN: -target x86_64-unknown-linux-gnu \
|
||||
// RUN: --offload-arch=gfx900 \
|
||||
// RUN: --offload-arch=gfx906 \
|
||||
// RUN: -c -nogpulib -fuse-cuid=hash \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
|
||||
// RUN: %S/Inputs/hip_multiple_inputs/b.hip \
|
||||
// RUN: 2>&1 | FileCheck -check-prefixes=COMMON,HEX %s
|
||||
|
||||
// INVALID: invalid value 'invalid' in '-fuse-cuid=invalid'
|
||||
|
||||
// COMMON: "{{.*}}clang{{.*}}" "-cc1"{{.*}} "-triple" "amdgcn-amd-amdhsa"
|
||||
// COMMON-SAME: "-target-cpu" "gfx900"
|
||||
// HEX-SAME: "-cuid=[[CUID:[0-9a-f]+]]"
|
||||
// FIXED-SAME: "-cuid=[[CUID:xyz_123]]"
|
||||
// COMMON-SAME: "{{.*}}a.cu"
|
||||
|
||||
// COMMON: "{{.*}}clang{{.*}}" "-cc1"{{.*}} "-triple" "amdgcn-amd-amdhsa"
|
||||
// COMMON-SAME: "-target-cpu" "gfx906"
|
||||
// COMMON-SAME: "-cuid=[[CUID]]"
|
||||
// COMMON-SAME: "{{.*}}a.cu"
|
||||
|
||||
// COMMON: "{{.*}}clang{{.*}}" "-cc1"{{.*}} "-triple" "x86_64-unknown-linux-gnu"
|
||||
// COMMON-SAME: "-cuid=[[CUID]]"
|
||||
// COMMON-SAME: "{{.*}}a.cu"
|
||||
|
||||
// COMMON: "{{.*}}clang{{.*}}" "-cc1"{{.*}} "-triple" "amdgcn-amd-amdhsa"
|
||||
// COMMON-SAME: "-target-cpu" "gfx900"
|
||||
// HEX-NOT: "-cuid=[[CUID]]"
|
||||
// HEX-SAME: "-cuid=[[CUID2:[0-9a-f]+]]"
|
||||
// FIXED-SAME: "-cuid=[[CUID2:xyz_123]]"
|
||||
// COMMON-SAME: "{{.*}}b.hip"
|
||||
|
||||
// COMMON: "{{.*}}clang{{.*}}" "-cc1"{{.*}} "-triple" "amdgcn-amd-amdhsa"
|
||||
// COMMON-SAME: "-target-cpu" "gfx906"
|
||||
// HEX-NOT: "-cuid=[[CUID]]"
|
||||
// COMMON-SAME: "-cuid=[[CUID2]]"
|
||||
// COMMON-SAME: "{{.*}}b.hip"
|
||||
|
||||
// COMMON: "{{.*}}clang{{.*}}" "-cc1"{{.*}} "-triple" "x86_64-unknown-linux-gnu"
|
||||
// HEX-NOT: "-cuid=[[CUID]]"
|
||||
// COMMON-SAME: "-cuid=[[CUID2]]"
|
||||
// COMMON-SAME: "{{.*}}b.hip"
|
Loading…
Reference in New Issue
Block a user