@@ -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
//
@@ -3156,19 +3203,15 @@ class OffloadingActionBuilder final {
3156
3203
// / Default GPU architecture if there's no one specified.
3157
3204
OffloadArch DefaultOffloadArch = OffloadArch::UNKNOWN;
3158
3205
3159
- // / Method to generate compilation unit ID specified by option
3160
- // / '-fuse-cuid='.
3161
- enum UseCUIDKind { CUID_Hash, CUID_Random, CUID_None, CUID_Invalid };
3162
- UseCUIDKind UseCUID = CUID_Hash;
3163
-
3164
- // / Compilation unit ID specified by option '-cuid='.
3165
- StringRef FixedCUID;
3206
+ // / Compilation unit ID specified by option '-fuse-cuid=' or'-cuid='.
3207
+ const CUIDOptions &CUIDOpts;
3166
3208
3167
3209
public:
3168
3210
CudaActionBuilderBase (Compilation &C, DerivedArgList &Args,
3169
3211
const Driver::InputList &Inputs,
3170
3212
Action::OffloadKind OFKind)
3171
- : DeviceActionBuilder(C, Args, Inputs, OFKind) {
3213
+ : DeviceActionBuilder(C, Args, Inputs, OFKind),
3214
+ CUIDOpts (C.getDriver().getCUIDOpts()) {
3172
3215
3173
3216
CompileDeviceOnly = C.getDriver ().offloadDeviceOnly ();
3174
3217
Relocatable = Args.hasFlag (options::OPT_fgpu_rdc,
@@ -3199,28 +3242,8 @@ class OffloadingActionBuilder final {
3199
3242
// Set the flag to true, so that the builder acts on the current input.
3200
3243
IsActive = true ;
3201
3244
3202
- std::string CUID = FixedCUID.str ();
3203
- if (CUID.empty ()) {
3204
- if (UseCUID == CUID_Random)
3205
- CUID = llvm::utohexstr (llvm::sys::Process::GetRandomNumber (),
3206
- /* LowerCase=*/ true );
3207
- else if (UseCUID == CUID_Hash) {
3208
- llvm::MD5 Hasher;
3209
- llvm::MD5::MD5Result Hash;
3210
- SmallString<256 > RealPath;
3211
- llvm::sys::fs::real_path (IA->getInputArg ().getValue (), RealPath,
3212
- /* expand_tilde=*/ true );
3213
- Hasher.update (RealPath);
3214
- for (auto *A : Args) {
3215
- if (A->getOption ().matches (options::OPT_INPUT))
3216
- continue ;
3217
- Hasher.update (A->getAsString (Args));
3218
- }
3219
- Hasher.final (Hash);
3220
- CUID = llvm::utohexstr (Hash.low (), /* LowerCase=*/ true );
3221
- }
3222
- }
3223
- IA->setId (CUID);
3245
+ if (CUIDOpts.isEnabled ())
3246
+ IA->setId (CUIDOpts.getCUID (IA->getInputArg ().getValue (), Args));
3224
3247
3225
3248
if (CompileHostOnly)
3226
3249
return ABRT_Success;
@@ -3346,21 +3369,6 @@ class OffloadingActionBuilder final {
3346
3369
CompileHostOnly = C.getDriver ().offloadHostOnly ();
3347
3370
EmitLLVM = Args.getLastArg (options::OPT_emit_llvm);
3348
3371
EmitAsm = Args.getLastArg (options::OPT_S);
3349
- FixedCUID = Args.getLastArgValue (options::OPT_cuid_EQ);
3350
- if (Arg *A = Args.getLastArg (options::OPT_fuse_cuid_EQ)) {
3351
- StringRef UseCUIDStr = A->getValue ();
3352
- UseCUID = llvm::StringSwitch<UseCUIDKind>(UseCUIDStr)
3353
- .Case (" hash" , CUID_Hash)
3354
- .Case (" random" , CUID_Random)
3355
- .Case (" none" , CUID_None)
3356
- .Default (CUID_Invalid);
3357
- if (UseCUID == CUID_Invalid) {
3358
- C.getDriver ().Diag (diag::err_drv_invalid_value)
3359
- << A->getAsString (Args) << UseCUIDStr;
3360
- C.setContainsError ();
3361
- return true ;
3362
- }
3363
- }
3364
3372
3365
3373
// --offload and --offload-arch options are mutually exclusive.
3366
3374
if (Args.hasArgNoClaim (options::OPT_offload_EQ) &&
@@ -4360,6 +4368,12 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
4360
4368
// Build the pipeline for this file.
4361
4369
Action *Current = C.MakeAction <InputAction>(*InputArg, InputType);
4362
4370
4371
+ std::string CUID;
4372
+ if (CUIDOpts.isEnabled () && types::isSrcFile (InputType)) {
4373
+ CUID = CUIDOpts.getCUID (InputArg->getValue (), Args);
4374
+ cast<InputAction>(Current)->setId (CUID);
4375
+ }
4376
+
4363
4377
// Use the current host action in any of the offloading actions, if
4364
4378
// required.
4365
4379
if (!UseNewOffloadingDriver)
@@ -4423,7 +4437,7 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
4423
4437
// Try to build the offloading actions and add the result as a dependency
4424
4438
// to the host.
4425
4439
if (UseNewOffloadingDriver)
4426
- Current = BuildOffloadingActions (C, Args, I, Current);
4440
+ Current = BuildOffloadingActions (C, Args, I, CUID, Current);
4427
4441
// Use the current host action in any of the offloading actions, if
4428
4442
// required.
4429
4443
else if (OffloadBuilder->addHostDependenceToDeviceActions (Current,
@@ -4760,7 +4774,7 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
4760
4774
4761
4775
Action *Driver::BuildOffloadingActions (Compilation &C,
4762
4776
llvm::opt::DerivedArgList &Args,
4763
- const InputTy &Input,
4777
+ const InputTy &Input, StringRef CUID,
4764
4778
Action *HostAction) const {
4765
4779
// Don't build offloading actions if explicitly disabled or we do not have a
4766
4780
// valid source input and compile action to embed it in. If preprocessing only
@@ -4801,13 +4815,13 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
4801
4815
llvm::DenseSet<StringRef> Arches = getOffloadArchs (C, Args, Kind, TC);
4802
4816
SmallVector<StringRef, 0 > Sorted (Arches.begin (), Arches.end ());
4803
4817
llvm::sort (Sorted);
4804
- for (StringRef Arch : Sorted)
4818
+ for (StringRef Arch : Sorted) {
4805
4819
TCAndArchs.push_back (std::make_pair (TC, Arch));
4820
+ DeviceActions.push_back (
4821
+ C.MakeAction <InputAction>(*InputArg, InputType, CUID));
4822
+ }
4806
4823
}
4807
4824
4808
- for (unsigned I = 0 , E = TCAndArchs.size (); I != E; ++I)
4809
- DeviceActions.push_back (C.MakeAction <InputAction>(*InputArg, InputType));
4810
-
4811
4825
if (DeviceActions.empty ())
4812
4826
return HostAction;
4813
4827
0 commit comments