-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang][NFC] Clean up OpenMP offload toolchain generation #145549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1030,10 +1030,6 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C, | |
| return; | ||
| } | ||
|
|
||
| llvm::StringMap<llvm::DenseSet<StringRef>> DerivedArchs; | ||
| llvm::StringMap<StringRef> FoundNormalizedTriples; | ||
| std::multiset<StringRef> OpenMPTriples; | ||
|
|
||
| // If the user specified -fopenmp-targets= we create a toolchain for each | ||
| // valid triple. Otherwise, if only --offload-arch= was specified we instead | ||
| // attempt to derive the appropriate toolchains from the arguments. | ||
|
|
@@ -1044,82 +1040,77 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C, | |
| << OpenMPTargets->getAsString(C.getInputArgs()); | ||
| return; | ||
| } | ||
|
|
||
| // Make sure these show up in a deterministic order. | ||
| std::multiset<StringRef> OpenMPTriples; | ||
| for (StringRef T : OpenMPTargets->getValues()) | ||
| OpenMPTriples.insert(T); | ||
|
|
||
| llvm::StringMap<StringRef> FoundNormalizedTriples; | ||
| for (StringRef T : OpenMPTriples) { | ||
| llvm::Triple TT(ToolChain::getOpenMPTriple(T)); | ||
| std::string NormalizedName = TT.normalize(); | ||
|
|
||
| // Make sure we don't have a duplicate triple. | ||
| auto [TripleIt, Inserted] = | ||
| FoundNormalizedTriples.try_emplace(NormalizedName, T); | ||
| if (!Inserted) { | ||
| Diag(clang::diag::warn_drv_omp_offload_target_duplicate) | ||
| << T << TripleIt->second; | ||
| continue; | ||
| } | ||
|
|
||
| // If the specified target is invalid, emit a diagnostic. | ||
| if (TT.getArch() == llvm::Triple::UnknownArch) { | ||
| Diag(clang::diag::err_drv_invalid_omp_target) << T; | ||
| continue; | ||
| } | ||
|
|
||
| auto &TC = getOffloadToolChain(C.getInputArgs(), Action::OFK_OpenMP, TT, | ||
| C.getDefaultToolChain().getTriple()); | ||
| C.addOffloadDeviceToolChain(&TC, Action::OFK_OpenMP); | ||
| } | ||
| } else if (C.getInputArgs().hasArg(options::OPT_offload_arch_EQ) && | ||
| ((!IsHIP && !IsCuda) || UseLLVMOffload)) { | ||
| const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); | ||
| auto AMDTriple = getHIPOffloadTargetTriple(*this, C.getInputArgs()); | ||
| auto NVPTXTriple = getNVIDIAOffloadTargetTriple(*this, C.getInputArgs(), | ||
| HostTC->getTriple()); | ||
| llvm::Triple AMDTriple("amdgcn-amd-amdhsa"); | ||
| llvm::Triple NVPTXTriple("nvptx64-nvidia-cuda"); | ||
|
|
||
| // Attempt to deduce the offloading triple from the set of architectures. | ||
| // We can only correctly deduce NVPTX / AMDGPU triples currently. | ||
| // We need to temporarily create these toolchains so that we can access | ||
| // tools for inferring architectures. | ||
| llvm::DenseSet<StringRef> Archs; | ||
| for (const std::optional<llvm::Triple> &TT : {NVPTXTriple, AMDTriple}) { | ||
| if (!TT) | ||
| continue; | ||
|
|
||
| auto &TC = | ||
| getOffloadToolChain(C.getInputArgs(), Action::OFK_OpenMP, *TT, | ||
| C.getDefaultToolChain().getTriple()); | ||
| for (StringRef Arch : | ||
| getOffloadArchs(C, C.getArgs(), Action::OFK_OpenMP, &TC, true)) | ||
| Archs.insert(Arch); | ||
| } | ||
| for (const llvm::Triple &TT : {AMDTriple, NVPTXTriple}) { | ||
| auto &TC = getOffloadToolChain(C.getInputArgs(), Action::OFK_OpenMP, TT, | ||
|
||
| C.getDefaultToolChain().getTriple()); | ||
|
|
||
| llvm::DenseSet<StringRef> Archs = | ||
| getOffloadArchs(C, C.getArgs(), Action::OFK_OpenMP, &TC, true); | ||
| llvm::DenseSet<StringRef> ArchsForTarget; | ||
| for (StringRef Arch : Archs) { | ||
| bool IsNVPTX = IsNVIDIAOffloadArch( | ||
| StringToOffloadArch(getProcessorFromTargetID(NVPTXTriple, Arch))); | ||
| bool IsAMDGPU = IsAMDOffloadArch( | ||
| StringToOffloadArch(getProcessorFromTargetID(AMDTriple, Arch))); | ||
| if (!IsNVPTX && !IsAMDGPU && !Arch.equals_insensitive("native")) { | ||
| Diag(clang::diag::err_drv_failed_to_deduce_target_from_arch) | ||
| << Arch; | ||
| return; | ||
| } | ||
|
|
||
| for (StringRef Arch : Archs) { | ||
| if (NVPTXTriple && IsNVIDIAOffloadArch(StringToOffloadArch( | ||
| getProcessorFromTargetID(*NVPTXTriple, Arch)))) { | ||
| DerivedArchs[NVPTXTriple->getTriple()].insert(Arch); | ||
| } else if (AMDTriple && | ||
| IsAMDOffloadArch(StringToOffloadArch( | ||
| getProcessorFromTargetID(*AMDTriple, Arch)))) { | ||
| DerivedArchs[AMDTriple->getTriple()].insert(Arch); | ||
| } else { | ||
| Diag(clang::diag::err_drv_failed_to_deduce_target_from_arch) << Arch; | ||
| return; | ||
| if (TT.isNVPTX() && IsNVPTX) | ||
| ArchsForTarget.insert(Arch); | ||
| else if (TT.isAMDGPU() && IsAMDGPU) | ||
| ArchsForTarget.insert(Arch); | ||
| } | ||
| if (!ArchsForTarget.empty()) { | ||
| C.addOffloadDeviceToolChain(&TC, Action::OFK_OpenMP); | ||
| KnownArchs[&TC] = ArchsForTarget; | ||
| } | ||
| } | ||
|
|
||
| // If the set is empty then we failed to find a native architecture. | ||
| if (Archs.empty()) { | ||
| auto TCRange = C.getOffloadToolChains(Action::OFK_OpenMP); | ||
|
||
| if (TCRange.first == TCRange.second) | ||
| Diag(clang::diag::err_drv_failed_to_deduce_target_from_arch) | ||
| << "native"; | ||
| return; | ||
| } | ||
|
|
||
| for (const auto &TripleAndArchs : DerivedArchs) | ||
| OpenMPTriples.insert(TripleAndArchs.first()); | ||
| } | ||
|
|
||
| for (StringRef Val : OpenMPTriples) { | ||
| llvm::Triple TT(ToolChain::getOpenMPTriple(Val)); | ||
| std::string NormalizedName = TT.normalize(); | ||
|
|
||
| // Make sure we don't have a duplicate triple. | ||
| auto [TripleIt, Inserted] = | ||
| FoundNormalizedTriples.try_emplace(NormalizedName, Val); | ||
| if (!Inserted) { | ||
| Diag(clang::diag::warn_drv_omp_offload_target_duplicate) | ||
| << Val << TripleIt->second; | ||
| continue; | ||
| } | ||
|
|
||
| // If the specified target is invalid, emit a diagnostic. | ||
| if (TT.getArch() == llvm::Triple::UnknownArch) { | ||
| Diag(clang::diag::err_drv_invalid_omp_target) << Val; | ||
| continue; | ||
| } | ||
|
|
||
| auto &TC = getOffloadToolChain(C.getInputArgs(), Action::OFK_OpenMP, TT, | ||
| C.getDefaultToolChain().getTriple()); | ||
| C.addOffloadDeviceToolChain(&TC, Action::OFK_OpenMP); | ||
| auto It = DerivedArchs.find(TT.getTriple()); | ||
| if (It != DerivedArchs.end()) | ||
| KnownArchs[&TC] = It->second; | ||
| } | ||
| } else if (C.getInputArgs().hasArg(options::OPT_fopenmp_targets_EQ)) { | ||
| Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no auto