@@ -197,6 +197,50 @@ std::string Driver::GetResourcesPath(StringRef BinaryPath) {
197
197
return std::string (P);
198
198
}
199
199
200
+ CUIDOptions::CUIDOptions (llvm::opt::DerivedArgList &Args, const Driver &D)
201
+ : UseCUID(Kind::Hash) {
202
+ if (Arg *A = Args.getLastArg (options::OPT_fuse_cuid_EQ)) {
203
+ StringRef UseCUIDStr = A->getValue ();
204
+ UseCUID = llvm::StringSwitch<Kind>(UseCUIDStr)
205
+ .Case (" hash" , Kind::Hash)
206
+ .Case (" random" , Kind::Random)
207
+ .Case (" none" , Kind::None)
208
+ .Default (Kind::Invalid);
209
+ if (UseCUID == Kind::Invalid)
210
+ D.Diag (clang::diag::err_drv_invalid_value)
211
+ << A->getAsString (Args) << UseCUIDStr;
212
+ }
213
+
214
+ FixedCUID = Args.getLastArgValue (options::OPT_cuid_EQ);
215
+ if (!FixedCUID.empty ())
216
+ UseCUID = Kind::Fixed;
217
+ }
218
+
219
+ std::string CUIDOptions::getCUID (StringRef InputFile,
220
+ llvm::opt::DerivedArgList &Args) const {
221
+ std::string CUID = FixedCUID.str ();
222
+ if (CUID.empty ()) {
223
+ if (UseCUID == Kind::Random)
224
+ CUID = llvm::utohexstr (llvm::sys::Process::GetRandomNumber (),
225
+ /* LowerCase=*/ true );
226
+ else if (UseCUID == Kind::Hash) {
227
+ llvm::MD5 Hasher;
228
+ llvm::MD5::MD5Result Hash;
229
+ SmallString<256 > RealPath;
230
+ llvm::sys::fs::real_path (InputFile, RealPath,
231
+ /* expand_tilde=*/ true );
232
+ Hasher.update (RealPath);
233
+ for (auto *A : Args) {
234
+ if (A->getOption ().matches (options::OPT_INPUT))
235
+ continue ;
236
+ Hasher.update (A->getAsString (Args));
237
+ }
238
+ Hasher.final (Hash);
239
+ CUID = llvm::utohexstr (Hash.low (), /* LowerCase=*/ true );
240
+ }
241
+ }
242
+ return CUID;
243
+ }
200
244
Driver::Driver (StringRef ClangExecutable, StringRef TargetTriple,
201
245
DiagnosticsEngine &Diags, std::string Title,
202
246
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
@@ -875,6 +919,9 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
875
919
C.addOffloadDeviceToolChain (HIPTC, OFK);
876
920
}
877
921
922
+ if (IsCuda || IsHIP)
923
+ CUIDOpts = CUIDOptions (C.getArgs (), *this );
924
+
878
925
//
879
926
// OpenMP
880
927
//
@@ -3161,19 +3208,15 @@ class OffloadingActionBuilder final {
3161
3208
// / Default GPU architecture if there's no one specified.
3162
3209
OffloadArch DefaultOffloadArch = OffloadArch::UNKNOWN;
3163
3210
3164
- // / Method to generate compilation unit ID specified by option
3165
- // / '-fuse-cuid='.
3166
- enum UseCUIDKind { CUID_Hash, CUID_Random, CUID_None, CUID_Invalid };
3167
- UseCUIDKind UseCUID = CUID_Hash;
3168
-
3169
- // / Compilation unit ID specified by option '-cuid='.
3170
- StringRef FixedCUID;
3211
+ // / Compilation unit ID specified by option '-fuse-cuid=' or'-cuid='.
3212
+ const CUIDOptions &CUIDOpts;
3171
3213
3172
3214
public:
3173
3215
CudaActionBuilderBase (Compilation &C, DerivedArgList &Args,
3174
3216
const Driver::InputList &Inputs,
3175
3217
Action::OffloadKind OFKind)
3176
- : DeviceActionBuilder(C, Args, Inputs, OFKind) {
3218
+ : DeviceActionBuilder(C, Args, Inputs, OFKind),
3219
+ CUIDOpts (C.getDriver().getCUIDOpts()) {
3177
3220
3178
3221
CompileDeviceOnly = C.getDriver ().offloadDeviceOnly ();
3179
3222
Relocatable = Args.hasFlag (options::OPT_fgpu_rdc,
@@ -3204,28 +3247,8 @@ class OffloadingActionBuilder final {
3204
3247
// Set the flag to true, so that the builder acts on the current input.
3205
3248
IsActive = true ;
3206
3249
3207
- std::string CUID = FixedCUID.str ();
3208
- if (CUID.empty ()) {
3209
- if (UseCUID == CUID_Random)
3210
- CUID = llvm::utohexstr (llvm::sys::Process::GetRandomNumber (),
3211
- /* LowerCase=*/ true );
3212
- else if (UseCUID == CUID_Hash) {
3213
- llvm::MD5 Hasher;
3214
- llvm::MD5::MD5Result Hash;
3215
- SmallString<256 > RealPath;
3216
- llvm::sys::fs::real_path (IA->getInputArg ().getValue (), RealPath,
3217
- /* expand_tilde=*/ true );
3218
- Hasher.update (RealPath);
3219
- for (auto *A : Args) {
3220
- if (A->getOption ().matches (options::OPT_INPUT))
3221
- continue ;
3222
- Hasher.update (A->getAsString (Args));
3223
- }
3224
- Hasher.final (Hash);
3225
- CUID = llvm::utohexstr (Hash.low (), /* LowerCase=*/ true );
3226
- }
3227
- }
3228
- IA->setId (CUID);
3250
+ if (CUIDOpts.isEnabled ())
3251
+ IA->setId (CUIDOpts.getCUID (IA->getInputArg ().getValue (), Args));
3229
3252
3230
3253
if (CompileHostOnly)
3231
3254
return ABRT_Success;
@@ -3351,21 +3374,6 @@ class OffloadingActionBuilder final {
3351
3374
CompileHostOnly = C.getDriver ().offloadHostOnly ();
3352
3375
EmitLLVM = Args.getLastArg (options::OPT_emit_llvm);
3353
3376
EmitAsm = Args.getLastArg (options::OPT_S);
3354
- FixedCUID = Args.getLastArgValue (options::OPT_cuid_EQ);
3355
- if (Arg *A = Args.getLastArg (options::OPT_fuse_cuid_EQ)) {
3356
- StringRef UseCUIDStr = A->getValue ();
3357
- UseCUID = llvm::StringSwitch<UseCUIDKind>(UseCUIDStr)
3358
- .Case (" hash" , CUID_Hash)
3359
- .Case (" random" , CUID_Random)
3360
- .Case (" none" , CUID_None)
3361
- .Default (CUID_Invalid);
3362
- if (UseCUID == CUID_Invalid) {
3363
- C.getDriver ().Diag (diag::err_drv_invalid_value)
3364
- << A->getAsString (Args) << UseCUIDStr;
3365
- C.setContainsError ();
3366
- return true ;
3367
- }
3368
- }
3369
3377
3370
3378
// --offload and --offload-arch options are mutually exclusive.
3371
3379
if (Args.hasArgNoClaim (options::OPT_offload_EQ) &&
@@ -4366,6 +4374,12 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
4366
4374
// Build the pipeline for this file.
4367
4375
Action *Current = C.MakeAction <InputAction>(*InputArg, InputType);
4368
4376
4377
+ std::string CUID;
4378
+ if (CUIDOpts.isEnabled () && types::isSrcFile (InputType)) {
4379
+ CUID = CUIDOpts.getCUID (InputArg->getValue (), Args);
4380
+ cast<InputAction>(Current)->setId (CUID);
4381
+ }
4382
+
4369
4383
// Use the current host action in any of the offloading actions, if
4370
4384
// required.
4371
4385
if (!UseNewOffloadingDriver)
@@ -4429,7 +4443,7 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
4429
4443
// Try to build the offloading actions and add the result as a dependency
4430
4444
// to the host.
4431
4445
if (UseNewOffloadingDriver)
4432
- Current = BuildOffloadingActions (C, Args, I, Current);
4446
+ Current = BuildOffloadingActions (C, Args, I, CUID, Current);
4433
4447
// Use the current host action in any of the offloading actions, if
4434
4448
// required.
4435
4449
else if (OffloadBuilder->addHostDependenceToDeviceActions (Current,
@@ -4766,7 +4780,7 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
4766
4780
4767
4781
Action *Driver::BuildOffloadingActions (Compilation &C,
4768
4782
llvm::opt::DerivedArgList &Args,
4769
- const InputTy &Input,
4783
+ const InputTy &Input, StringRef CUID,
4770
4784
Action *HostAction) const {
4771
4785
// Don't build offloading actions if explicitly disabled or we do not have a
4772
4786
// valid source input and compile action to embed it in. If preprocessing only
@@ -4807,13 +4821,13 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
4807
4821
llvm::DenseSet<StringRef> Arches = getOffloadArchs (C, Args, Kind, TC);
4808
4822
SmallVector<StringRef, 0 > Sorted (Arches.begin (), Arches.end ());
4809
4823
llvm::sort (Sorted);
4810
- for (StringRef Arch : Sorted)
4824
+ for (StringRef Arch : Sorted) {
4811
4825
TCAndArchs.push_back (std::make_pair (TC, Arch));
4826
+ DeviceActions.push_back (
4827
+ C.MakeAction <InputAction>(*InputArg, InputType, CUID));
4828
+ }
4812
4829
}
4813
4830
4814
- for (unsigned I = 0 , E = TCAndArchs.size (); I != E; ++I)
4815
- DeviceActions.push_back (C.MakeAction <InputAction>(*InputArg, InputType));
4816
-
4817
4831
if (DeviceActions.empty ())
4818
4832
return HostAction;
4819
4833
0 commit comments