Skip to content

Commit de34ac1

Browse files
committed
[AMDGPU] Use the AMDGPUToolChain when targeting C/C++ directly
Summary: The `getToolChain` pass uses the triple to determine which toolchain to create. Currently the `amdgcn-amd-amdhsa` triple maps to the `ROCmToolChain` which uses things expected to be provided by `ROCm`. This is neded for OpenCL, but directly targeting C++ does not want this since it's primarily being used for creating GPU runtime code. As far as I know I'm the only user of this, so this shouldn't change anything. Unfortunately, there's no good logic for detercting this, so I simply checked ahead of time if the input is either `foo.cl` or `-x cl foo.c` to choose between the two. This allows us to use the AMDGPU target normally, as otherwise it will error without passing `-nogpulib`. Refactor to use inputlist delete old
1 parent 00f2989 commit de34ac1

File tree

7 files changed

+47
-47
lines changed

7 files changed

+47
-47
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,11 @@ class Driver {
468468
/// BuildInputs - Construct the list of inputs and their types from
469469
/// the given arguments.
470470
///
471-
/// \param TC - The default host tool chain.
471+
/// \param TT - The target triple.
472472
/// \param Args - The input arguments.
473473
/// \param Inputs - The list to store the resulting compilation
474474
/// inputs onto.
475-
void BuildInputs(const ToolChain &TC, llvm::opt::DerivedArgList &Args,
475+
void BuildInputs(const llvm::Triple &TT, llvm::opt::DerivedArgList &Args,
476476
InputList &Inputs) const;
477477

478478
/// BuildActions - Construct the list of actions to perform for the
@@ -758,7 +758,8 @@ class Driver {
758758
/// Will cache ToolChains for the life of the driver object, and create them
759759
/// on-demand.
760760
const ToolChain &getToolChain(const llvm::opt::ArgList &Args,
761-
const llvm::Triple &Target) const;
761+
const llvm::Triple &Target,
762+
const InputList &Inputs) const;
762763

763764
/// @}
764765

clang/include/clang/Driver/ToolChain.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,6 @@ class ToolChain {
422422
/// native LLVM support.
423423
virtual bool HasNativeLLVMSupport() const;
424424

425-
/// LookupTypeForExtension - Return the default language type to use for the
426-
/// given extension.
427-
virtual types::ID LookupTypeForExtension(StringRef Ext) const;
428-
429425
/// IsBlocksDefault - Does this tool chain enable -fblocks by default.
430426
virtual bool IsBlocksDefault() const { return false; }
431427

clang/lib/Driver/Driver.cpp

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
982982

983983
TC = DeviceTC.get();
984984
} else
985-
TC = &getToolChain(C.getInputArgs(), TT);
985+
TC = &getToolChain(C.getInputArgs(), TT, Inputs);
986986
C.addOffloadDeviceToolChain(TC, Action::OFK_OpenMP);
987987
auto It = DerivedArchs.find(TT.getTriple());
988988
if (It != DerivedArchs.end())
@@ -1484,12 +1484,17 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
14841484
std::unique_ptr<llvm::opt::InputArgList> UArgs =
14851485
std::make_unique<InputArgList>(std::move(Args));
14861486

1487+
llvm::Triple TT = computeTargetTriple(*this, TargetTriple, *UArgs);
1488+
14871489
// Perform the default argument translations.
14881490
DerivedArgList *TranslatedArgs = TranslateInputArgs(*UArgs);
14891491

1492+
// Construct the list of inputs.
1493+
InputList Inputs;
1494+
BuildInputs(TT, *TranslatedArgs, Inputs);
1495+
14901496
// Owned by the host.
1491-
const ToolChain &TC = getToolChain(
1492-
*UArgs, computeTargetTriple(*this, TargetTriple, *UArgs));
1497+
const ToolChain &TC = getToolChain(*UArgs, TT, Inputs);
14931498

14941499
// Check if the environment version is valid except wasm case.
14951500
llvm::Triple Triple = TC.getTriple();
@@ -1550,10 +1555,6 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
15501555
if (!HandleImmediateArgs(*C))
15511556
return C;
15521557

1553-
// Construct the list of inputs.
1554-
InputList Inputs;
1555-
BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs);
1556-
15571558
// Populate the tool chains for the offloading devices, if any.
15581559
CreateOffloadingDeviceToolChains(*C, Inputs);
15591560

@@ -1767,7 +1768,7 @@ void Driver::generateCompilationDiagnostics(
17671768

17681769
// Construct the list of inputs.
17691770
InputList Inputs;
1770-
BuildInputs(C.getDefaultToolChain(), C.getArgs(), Inputs);
1771+
BuildInputs(C.getDefaultToolChain().getTriple(), C.getArgs(), Inputs);
17711772

17721773
for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) {
17731774
bool IgnoreInput = false;
@@ -2660,8 +2661,24 @@ static types::ID CXXHeaderUnitType(ModuleHeaderMode HM) {
26602661
return types::TY_CXXHUHeader;
26612662
}
26622663

2664+
static types::ID lookupTypeForExtension(const Driver &D, const llvm::Triple &TT,
2665+
llvm::StringRef Ext) {
2666+
types::ID Ty = types::lookupTypeForExtension(Ext);
2667+
2668+
// Flang always runs the preprocessor and has no notion of "preprocessed
2669+
// fortran". Here, TY_PP_Fortran is coerced to TY_Fortran to avoid treating
2670+
// them differently.
2671+
if (D.IsFlangMode() && Ty == types::TY_PP_Fortran)
2672+
Ty = types::TY_Fortran;
2673+
2674+
// Darwin always preprocesses assembly files (unless -x is used explicitly).
2675+
if (TT.isOSBinFormatMachO() && Ty == types::TY_PP_Asm)
2676+
Ty = types::TY_Asm;
2677+
return Ty;
2678+
}
2679+
26632680
// Construct a the list of inputs and their types.
2664-
void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
2681+
void Driver::BuildInputs(const llvm::Triple &TT, DerivedArgList &Args,
26652682
InputList &Inputs) const {
26662683
const llvm::opt::OptTable &Opts = getOpts();
26672684
// Track the current user specified (-x) input. We also explicitly track the
@@ -2739,7 +2756,7 @@ void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
27392756
// We use a host hook here because Darwin at least has its own
27402757
// idea of what .s is.
27412758
if (const char *Ext = strrchr(Value, '.'))
2742-
Ty = TC.LookupTypeForExtension(Ext + 1);
2759+
Ty = lookupTypeForExtension(*this, TT, Ext + 1);
27432760

27442761
if (Ty == types::TY_INVALID) {
27452762
if (IsCLMode() && (Args.hasArgNoClaim(options::OPT_E) || CCGenDiagnostics))
@@ -2795,7 +2812,8 @@ void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
27952812
// If emulating cl.exe, make sure that /TC and /TP don't affect input
27962813
// object files.
27972814
const char *Ext = strrchr(Value, '.');
2798-
if (Ext && TC.LookupTypeForExtension(Ext + 1) == types::TY_Object)
2815+
if (Ext &&
2816+
lookupTypeForExtension(*this, TT, Ext + 1) == types::TY_Object)
27992817
Ty = types::TY_Object;
28002818
}
28012819
if (Ty == types::TY_INVALID) {
@@ -5613,9 +5631,9 @@ InputInfoList Driver::BuildJobsForActionNoCache(
56135631
StringRef ArchName = BAA->getArchName();
56145632

56155633
if (!ArchName.empty())
5616-
TC = &getToolChain(C.getArgs(),
5617-
computeTargetTriple(*this, TargetTriple,
5618-
C.getArgs(), ArchName));
5634+
TC = &getToolChain(
5635+
C.getArgs(),
5636+
computeTargetTriple(*this, TargetTriple, C.getArgs(), ArchName), {});
56195637
else
56205638
TC = &C.getDefaultToolChain();
56215639

@@ -6376,7 +6394,8 @@ std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const {
63766394
}
63776395

63786396
const ToolChain &Driver::getToolChain(const ArgList &Args,
6379-
const llvm::Triple &Target) const {
6397+
const llvm::Triple &Target,
6398+
const InputList &Inputs) const {
63806399

63816400
auto &TC = ToolChains[Target.str()];
63826401
if (!TC) {
@@ -6444,7 +6463,12 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
64446463
TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
64456464
break;
64466465
case llvm::Triple::AMDHSA:
6447-
TC = std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args);
6466+
TC =
6467+
llvm::any_of(Inputs,
6468+
[](auto &Input) { return types::isOpenCL(Input.first); })
6469+
? std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args)
6470+
: std::make_unique<toolchains::AMDGPUToolChain>(*this, Target,
6471+
Args);
64486472
break;
64496473
case llvm::Triple::AMDPAL:
64506474
case llvm::Triple::Mesa3D:

clang/lib/Driver/ToolChain.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,18 +1035,6 @@ std::string ToolChain::GetStaticLibToolPath() const {
10351035
return GetProgramPath("llvm-ar");
10361036
}
10371037

1038-
types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const {
1039-
types::ID id = types::lookupTypeForExtension(Ext);
1040-
1041-
// Flang always runs the preprocessor and has no notion of "preprocessed
1042-
// fortran". Here, TY_PP_Fortran is coerced to TY_Fortran to avoid treating
1043-
// them differently.
1044-
if (D.IsFlangMode() && id == types::TY_PP_Fortran)
1045-
id = types::TY_Fortran;
1046-
1047-
return id;
1048-
}
1049-
10501038
bool ToolChain::HasNativeLLVMSupport() const {
10511039
return false;
10521040
}

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -967,16 +967,6 @@ Darwin::Darwin(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
967967
: MachO(D, Triple, Args), TargetInitialized(false),
968968
CudaInstallation(D, Triple, Args), RocmInstallation(D, Triple, Args) {}
969969

970-
types::ID MachO::LookupTypeForExtension(StringRef Ext) const {
971-
types::ID Ty = ToolChain::LookupTypeForExtension(Ext);
972-
973-
// Darwin always preprocesses assembly files (unless -x is used explicitly).
974-
if (Ty == types::TY_PP_Asm)
975-
return types::TY_Asm;
976-
977-
return Ty;
978-
}
979-
980970
bool MachO::HasNativeLLVMSupport() const { return true; }
981971

982972
ToolChain::CXXStdlibType Darwin::GetDefaultCXXStdlibType() const {

clang/lib/Driver/ToolChains/Darwin.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,6 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
234234
/// @name ToolChain Implementation
235235
/// {
236236

237-
types::ID LookupTypeForExtension(StringRef Ext) const override;
238-
239237
bool HasNativeLLVMSupport() const override;
240238

241239
llvm::opt::DerivedArgList *

clang/test/Driver/amdgpu-toolchain.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@
3636
// RUN: %clang -target amdgcn-amd-amdhsa -march=gfx90a -stdlib -startfiles \
3737
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
3838
// STARTUP: ld.lld{{.*}}"-lc" "-lm" "{{.*}}crt1.o"
39+
40+
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 %s 2>&1 | FileCheck -check-prefix=ROCM %s
41+
// ROCM-NOT: -mlink-builtin-bitcode

0 commit comments

Comments
 (0)