Skip to content

Commit 0b81d9a

Browse files
committed
[AMDGPU] add -mcode-object-version=n
Add option -mcode-object-version=n to control code object version for AMDGPU. Differential Revision: https://reviews.llvm.org/D91310
1 parent 5cae708 commit 0b81d9a

22 files changed

+203
-74
lines changed

clang/docs/ClangCommandLineReference.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,6 +2663,10 @@ Align selected branches (fused, jcc, jmp) within 32-byte boundary
26632663

26642664
Legacy option to specify code object ABI V2 (-mnocode-object-v3) or V3 (-mcode-object-v3) (AMDGPU only)
26652665

2666+
.. option:: -mcode-object-version=<version>
2667+
2668+
Specify code object ABI version. Defaults to 4. (AMDGPU only)
2669+
26662670
.. option:: -mconsole<arg>
26672671

26682672
.. program:: clang1

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,6 +2560,10 @@ def mexec_model_EQ : Joined<["-"], "mexec-model=">, Group<m_wasm_Features_Driver
25602560
Values<"command,reactor">,
25612561
HelpText<"Execution model (WebAssembly only)">;
25622562

2563+
def mcode_object_version_EQ : Joined<["-"], "mcode-object-version=">, Group<m_Group>,
2564+
HelpText<"Specify code object ABI version. Defaults to 4. (AMDGPU only)">,
2565+
MetaVarName<"<version>">, Values<"2,3,4">;
2566+
25632567
def mcode_object_v3_legacy : Flag<["-"], "mcode-object-v3">, Group<m_Group>,
25642568
HelpText<"Legacy option to specify code object ABI V2 (-mnocode-object-v3) or V3 (-mcode-object-v3) (AMDGPU only)">;
25652569
def mno_code_object_v3_legacy : Flag<["-"], "mno-code-object-v3">, Group<m_Group>;

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,14 @@ void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
399399
AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
400400
const ArgList &Args)
401401
: Generic_ELF(D, Triple, Args),
402-
OptionsDefault({{options::OPT_O, "3"},
403-
{options::OPT_cl_std_EQ, "CL1.2"}}) {}
402+
OptionsDefault(
403+
{{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}) {
404+
// Check code object version options. Emit warnings for legacy options
405+
// and errors for the last invalid code object version options.
406+
// It is done here to avoid repeated warning or error messages for
407+
// each tool invocation.
408+
(void)getOrCheckAMDGPUCodeObjectVersion(D, Args, /*Diagnose=*/true);
409+
}
404410

405411
Tool *AMDGPUToolChain::buildLinker() const {
406412
return new tools::amdgpu::Linker(*this);

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,24 +1064,14 @@ static const char *RelocationModelName(llvm::Reloc::Model Model) {
10641064
}
10651065
llvm_unreachable("Unknown Reloc::Model kind");
10661066
}
1067-
1068-
static void HandleAmdgcnLegacyOptions(const Driver &D,
1069-
const ArgList &Args,
1070-
ArgStringList &CmdArgs) {
1071-
if (auto *CodeObjArg = Args.getLastArg(options::OPT_mcode_object_v3_legacy,
1072-
options::OPT_mno_code_object_v3_legacy)) {
1073-
if (CodeObjArg->getOption().getID() == options::OPT_mcode_object_v3_legacy) {
1074-
D.Diag(diag::warn_drv_deprecated_arg) << "-mcode-object-v3" <<
1075-
"-mllvm --amdhsa-code-object-version=3";
1076-
CmdArgs.push_back("-mllvm");
1077-
CmdArgs.push_back("--amdhsa-code-object-version=3");
1078-
} else {
1079-
D.Diag(diag::warn_drv_deprecated_arg) << "-mno-code-object-v3" <<
1080-
"-mllvm --amdhsa-code-object-version=2";
1081-
CmdArgs.push_back("-mllvm");
1082-
CmdArgs.push_back("--amdhsa-code-object-version=2");
1083-
}
1084-
}
1067+
static void handleAMDGPUCodeObjectVersionOptions(const Driver &D,
1068+
const ArgList &Args,
1069+
ArgStringList &CmdArgs) {
1070+
unsigned CodeObjVer = getOrCheckAMDGPUCodeObjectVersion(D, Args);
1071+
CmdArgs.insert(CmdArgs.begin() + 1,
1072+
Args.MakeArgString(Twine("--amdhsa-code-object-version=") +
1073+
Twine(CodeObjVer)));
1074+
CmdArgs.insert(CmdArgs.begin() + 1, "-mllvm");
10851075
}
10861076

10871077
void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
@@ -6243,8 +6233,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
62436233
}
62446234
}
62456235

6246-
HandleAmdgcnLegacyOptions(D, Args, CmdArgs);
62476236
if (Triple.isAMDGPU()) {
6237+
handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs);
6238+
62486239
if (Args.hasFlag(options::OPT_munsafe_fp_atomics,
62496240
options::OPT_mno_unsafe_fp_atomics))
62506241
CmdArgs.push_back("-munsafe-fp-atomics");
@@ -7229,7 +7220,8 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
72297220
CmdArgs.push_back(SplitDebugName(JA, Args, Input, Output));
72307221
}
72317222

7232-
HandleAmdgcnLegacyOptions(D, Args, CmdArgs);
7223+
if (Triple.isAMDGPU())
7224+
handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs);
72337225

72347226
assert(Input.isFilename() && "Invalid input.");
72357227
CmdArgs.push_back(Input.getFilename());

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,3 +1542,44 @@ void tools::addX86AlignBranchArgs(const Driver &D, const ArgList &Args,
15421542
}
15431543
}
15441544
}
1545+
1546+
unsigned tools::getOrCheckAMDGPUCodeObjectVersion(
1547+
const Driver &D, const llvm::opt::ArgList &Args, bool Diagnose) {
1548+
const unsigned MinCodeObjVer = 2;
1549+
const unsigned MaxCodeObjVer = 4;
1550+
unsigned CodeObjVer = 4;
1551+
1552+
// Emit warnings for legacy options even if they are overridden.
1553+
if (Diagnose) {
1554+
if (Args.hasArg(options::OPT_mno_code_object_v3_legacy))
1555+
D.Diag(diag::warn_drv_deprecated_arg) << "-mno-code-object-v3"
1556+
<< "-mcode-object-version=2";
1557+
1558+
if (Args.hasArg(options::OPT_mcode_object_v3_legacy))
1559+
D.Diag(diag::warn_drv_deprecated_arg) << "-mcode-object-v3"
1560+
<< "-mcode-object-version=3";
1561+
}
1562+
1563+
// The last of -mcode-object-v3, -mno-code-object-v3 and
1564+
// -mcode-object-version=<version> wins.
1565+
if (auto *CodeObjArg =
1566+
Args.getLastArg(options::OPT_mcode_object_v3_legacy,
1567+
options::OPT_mno_code_object_v3_legacy,
1568+
options::OPT_mcode_object_version_EQ)) {
1569+
if (CodeObjArg->getOption().getID() ==
1570+
options::OPT_mno_code_object_v3_legacy) {
1571+
CodeObjVer = 2;
1572+
} else if (CodeObjArg->getOption().getID() ==
1573+
options::OPT_mcode_object_v3_legacy) {
1574+
CodeObjVer = 3;
1575+
} else {
1576+
auto Remnant =
1577+
StringRef(CodeObjArg->getValue()).getAsInteger(0, CodeObjVer);
1578+
if (Diagnose &&
1579+
(Remnant || CodeObjVer < MinCodeObjVer || CodeObjVer > MaxCodeObjVer))
1580+
D.Diag(diag::err_drv_invalid_int_value)
1581+
<< CodeObjArg->getAsString(Args) << CodeObjArg->getValue();
1582+
}
1583+
}
1584+
return CodeObjVer;
1585+
}

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ void addMultilibFlag(bool Enabled, const char *const Flag,
137137

138138
void addX86AlignBranchArgs(const Driver &D, const llvm::opt::ArgList &Args,
139139
llvm::opt::ArgStringList &CmdArgs, bool IsLTO);
140+
141+
unsigned getOrCheckAMDGPUCodeObjectVersion(const Driver &D,
142+
const llvm::opt::ArgList &Args,
143+
bool Diagnose = false);
140144
} // end namespace tools
141145
} // end namespace driver
142146
} // end namespace clang

clang/lib/Driver/ToolChains/HIP.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,15 @@ void AMDGCN::constructHIPFatbinCommand(Compilation &C, const JobAction &JA,
118118
std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux";
119119
std::string BundlerInputArg = "-inputs=" NULL_FILE;
120120

121+
// TODO: Change the bundle ID as requested by HIP runtime.
122+
// For code object version 2 and 3, the offload kind in bundle ID is 'hip'
123+
// for backward compatibility. For code object version 4 and greater, the
124+
// offload kind in bundle ID is 'hipv4'.
125+
std::string OffloadKind = "hip";
121126
for (const auto &II : Inputs) {
122127
const auto* A = II.getAction();
123-
BundlerTargetArg = BundlerTargetArg + ",hip-amdgcn-amd-amdhsa--" +
128+
BundlerTargetArg = BundlerTargetArg + "," + OffloadKind +
129+
"-amdgcn-amd-amdhsa--" +
124130
StringRef(A->getOffloadingArch()).str();
125131
BundlerInputArg = BundlerInputArg + "," + II.getFilename();
126132
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// RUN: %clang -### -target amdgcn-amd-amdhsa -mcpu=gfx900 -mcode-object-v3 %s 2>&1 | FileCheck --check-prefix=CODE-OBJECT-V3 %s
2-
// CODE-OBJECT-V3: warning: argument '-mcode-object-v3' is deprecated, use '-mllvm --amdhsa-code-object-version=3' instead [-Wdeprecated]
2+
// CODE-OBJECT-V3: warning: argument '-mcode-object-v3' is deprecated, use '-mcode-object-version=3' instead [-Wdeprecated]
33
// CODE-OBJECT-V3: "-mllvm" "--amdhsa-code-object-version=3"
44

55
// RUN: %clang -### -target amdgcn-amd-amdhsa amdgcn -mcpu=gfx900 -mno-code-object-v3 %s 2>&1 | FileCheck --check-prefix=NO-CODE-OBJECT-V3 %s
6-
// NO-CODE-OBJECT-V3: warning: argument '-mno-code-object-v3' is deprecated, use '-mllvm --amdhsa-code-object-version=2' instead [-Wdeprecated]
6+
// NO-CODE-OBJECT-V3: warning: argument '-mno-code-object-v3' is deprecated, use '-mcode-object-version=2' instead [-Wdeprecated]
77
// NO-CODE-OBJECT-V3: "-mllvm" "--amdhsa-code-object-version=2"
88

99
// RUN: %clang -### -target amdgcn-amd-amdhsa -mcpu=gfx900 -mcode-object-v3 -mno-code-object-v3 -mcode-object-v3 %s 2>&1 | FileCheck --check-prefix=MUL-CODE-OBJECT-V3 %s
10-
// MUL-CODE-OBJECT-V3: warning: argument '-mcode-object-v3' is deprecated, use '-mllvm --amdhsa-code-object-version=3' instead [-Wdeprecated]
10+
// MUL-CODE-OBJECT-V3: warning: argument '-mcode-object-v3' is deprecated, use '-mcode-object-version=3' instead [-Wdeprecated]
1111
// MUL-CODE-OBJECT-V3: "-mllvm" "--amdhsa-code-object-version=3"

clang/test/Driver/amdgpu-features.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// RUN: %clang -### -target amdgcn-amd-amdhsa -mcpu=gfx700 -mcode-object-v3 %s 2>&1 | FileCheck --check-prefix=CODE-OBJECT-V3 %s
2-
// CODE-OBJECT-V3: warning: argument '-mcode-object-v3' is deprecated, use '-mllvm --amdhsa-code-object-version=3' instead [-Wdeprecated]
2+
// CODE-OBJECT-V3: warning: argument '-mcode-object-v3' is deprecated, use '-mcode-object-version=3' instead [-Wdeprecated]
33
// CODE-OBJECT-V3: "-mllvm" "--amdhsa-code-object-version=3"
44

55
// RUN: %clang -### -target amdgcn-amd-amdhsa amdgcn -mcpu=gfx700 -mno-code-object-v3 %s 2>&1 | FileCheck --check-prefix=NO-CODE-OBJECT-V3 %s
6-
// NO-CODE-OBJECT-V3: warning: argument '-mno-code-object-v3' is deprecated, use '-mllvm --amdhsa-code-object-version=2' instead [-Wdeprecated]
6+
// NO-CODE-OBJECT-V3: warning: argument '-mno-code-object-v3' is deprecated, use '-mcode-object-version=2' instead [-Wdeprecated]
77
// NO-CODE-OBJECT-V3: "-mllvm" "--amdhsa-code-object-version=2"
88

99
// RUN: %clang -### -target amdgcn-amd-amdhsa -mcpu=gfx700 -mcode-object-v3 -mno-code-object-v3 -mcode-object-v3 %s 2>&1 | FileCheck --check-prefix=MUL-CODE-OBJECT-V3 %s
10-
// MUL-CODE-OBJECT-V3: warning: argument '-mcode-object-v3' is deprecated, use '-mllvm --amdhsa-code-object-version=3' instead [-Wdeprecated]
10+
// MUL-CODE-OBJECT-V3: warning: argument '-mcode-object-v3' is deprecated, use '-mcode-object-version=3' instead [-Wdeprecated]
1111
// MUL-CODE-OBJECT-V3: "-mllvm" "--amdhsa-code-object-version=3"
1212

1313
// RUN: %clang -### -target amdgcn-amdhsa -mcpu=gfx900:xnack+ %s 2>&1 | FileCheck --check-prefix=XNACK %s

clang/test/Driver/hip-autolink.hip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
88
// RUN: --cuda-host-only %s -### 2>&1 | FileCheck --check-prefix=HOST %s
99

10-
// DEV: "-cc1" "-triple" "amdgcn-amd-amdhsa"
10+
// DEV: "-cc1" "-mllvm" "--amdhsa-code-object-version=4" "-triple" "amdgcn-amd-amdhsa"
1111
// DEV-SAME: "-fno-autolink"
1212

1313
// HOST: "-cc1" "-triple" "i386-pc-windows-msvc{{.*}}"
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// REQUIRES: clang-driver, amdgpu-registered-target
2+
3+
// Check bundle ID for code object v3.
4+
5+
// RUN: %clang -### -target x86_64-linux-gnu \
6+
// RUN: -mcode-object-v3 \
7+
// RUN: --offload-arch=gfx906 -nogpulib \
8+
// RUN: %s 2>&1 | FileCheck -check-prefixes=V3,V3-WARN %s
9+
10+
// RUN: %clang -### -target x86_64-linux-gnu \
11+
// RUN: -mcode-object-version=3 \
12+
// RUN: --offload-arch=gfx906 -nogpulib \
13+
// RUN: %s 2>&1 | FileCheck -check-prefix=V3 %s
14+
15+
// RUN: %clang -### -target x86_64-linux-gnu \
16+
// RUN: -mcode-object-version=4 -mcode-object-version=3 \
17+
// RUN: --offload-arch=gfx906 -nogpulib \
18+
// RUN: %s 2>&1 | FileCheck -check-prefix=V3 %s
19+
20+
// V3-WARN: warning: argument '-mcode-object-v3' is deprecated, use '-mcode-object-version=3' instead [-Wdeprecated]
21+
// V3: "-mllvm" "--amdhsa-code-object-version=3"
22+
// V3: "-targets=host-x86_64-unknown-linux,hip-amdgcn-amd-amdhsa--gfx906"
23+
24+
// Check bundle ID for code object v2.
25+
26+
// RUN: %clang -### -target x86_64-linux-gnu \
27+
// RUN: -mno-code-object-v3 \
28+
// RUN: --offload-arch=gfx906 -nogpulib \
29+
// RUN: %s 2>&1 | FileCheck -check-prefixes=V2,V2-WARN %s
30+
31+
// RUN: %clang -### -target x86_64-linux-gnu \
32+
// RUN: -mcode-object-version=2 \
33+
// RUN: --offload-arch=gfx906 -nogpulib \
34+
// RUN: %s 2>&1 | FileCheck -check-prefix=V2 %s
35+
36+
// V2-WARN: warning: argument '-mno-code-object-v3' is deprecated, use '-mcode-object-version=2' instead [-Wdeprecated]
37+
// V2: "-mllvm" "--amdhsa-code-object-version=2"
38+
// V2: "-targets=host-x86_64-unknown-linux,hip-amdgcn-amd-amdhsa--gfx906"
39+
40+
// Check bundle ID for code object version 4.
41+
42+
// RUN: %clang -### -target x86_64-linux-gnu \
43+
// RUN: -mcode-object-version=4 \
44+
// RUN: --offload-arch=gfx906 -nogpulib \
45+
// RUN: %s 2>&1 | FileCheck -check-prefix=V4 %s
46+
47+
// RUN: %clang -### -target x86_64-linux-gnu \
48+
// RUN: --offload-arch=gfx906 -nogpulib \
49+
// RUN: %s 2>&1 | FileCheck -check-prefix=V4 %s
50+
51+
// V4: "-mllvm" "--amdhsa-code-object-version=4"
52+
// V4: "-targets=host-x86_64-unknown-linux,hip-amdgcn-amd-amdhsa--gfx906"
53+
54+
// Check invalid code object version option.
55+
56+
// RUN: %clang -### -target x86_64-linux-gnu \
57+
// RUN: -mcode-object-version=1 \
58+
// RUN: --offload-arch=gfx906 -nogpulib \
59+
// RUN: %s 2>&1 | FileCheck -check-prefix=INVALID %s
60+
// INVALID: error: invalid integral value '1' in '-mcode-object-version=1'
61+
// INVALID-NOT: error: invalid integral value
62+
63+
// Check warnings are emitted for legacy options before -mcode-object-version options.
64+
// Check warnings are emitted only once.
65+
66+
// RUN: %clang -### -target x86_64-linux-gnu \
67+
// RUN: -mno-code-object-v3 -mcode-object-v3 -mcode-object-version=4 \
68+
// RUN: --offload-arch=gfx906 -nogpulib \
69+
// RUN: %s 2>&1 | FileCheck -check-prefixes=WARN %s
70+
// WARN: warning: argument '-mno-code-object-v3' is deprecated, use '-mcode-object-version=2' instead [-Wdeprecated]
71+
// WARN: warning: argument '-mcode-object-v3' is deprecated, use '-mcode-object-version=3' instead [-Wdeprecated]
72+
// WARN-NOT: warning: argument {{.*}} is deprecated

clang/test/Driver/hip-device-compile.hip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
2727
// RUN: 2>&1 | FileCheck -check-prefixes=CHECK,ASM %s
2828

29-
// CHECK: {{".*clang.*"}} "-cc1" "-triple" "amdgcn-amd-amdhsa"
29+
// CHECK: {{".*clang.*"}} "-cc1" "-mllvm" "--amdhsa-code-object-version=4" "-triple" "amdgcn-amd-amdhsa"
3030
// CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu"
3131
// BC-SAME: "-emit-llvm-bc"
3232
// LL-SAME: "-emit-llvm"

clang/test/Driver/hip-host-cpu-features.hip

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
// RUN: %clang -### -c -target x86_64-linux-gnu -msse3 --cuda-gpu-arch=gfx803 -nogpulib %s 2>&1 | FileCheck %s -check-prefix=HOSTSSE3
77
// RUN: %clang -### -c -target x86_64-linux-gnu --gpu-use-aux-triple-only -march=znver2 --cuda-gpu-arch=gfx803 -nogpulib %s 2>&1 | FileCheck %s -check-prefix=NOHOSTCPU
88

9-
// HOSTCPU: "-cc1" "-triple" "amdgcn-amd-amdhsa"
9+
// HOSTCPU: "-cc1" "-mllvm" "--amdhsa-code-object-version=4" "-triple" "amdgcn-amd-amdhsa"
1010
// HOSTCPU-SAME: "-aux-triple" "x86_64-unknown-linux-gnu"
1111
// HOSTCPU-SAME: "-aux-target-cpu" "znver2"
1212

13-
// HOSTSSE3: "-cc1" "-triple" "amdgcn-amd-amdhsa"
13+
// HOSTSSE3: "-cc1" "-mllvm" "--amdhsa-code-object-version=4" "-triple" "amdgcn-amd-amdhsa"
1414
// HOSTSSE3-SAME: "-aux-triple" "x86_64-unknown-linux-gnu"
1515
// HOSTSSE3-SAME: "-aux-target-feature" "+sse3"
1616

17-
// NOHOSTCPU: "-cc1" "-triple" "amdgcn-amd-amdhsa"
17+
// NOHOSTCPU: "-cc1" "-mllvm" "--amdhsa-code-object-version=4" "-triple" "amdgcn-amd-amdhsa"
1818
// NOHOSTCPU-SAME: "-aux-triple" "x86_64-unknown-linux-gnu"
1919
// NOHOSTCPU-NOT: "-aux-target-cpu" "znver2"

0 commit comments

Comments
 (0)