[Flang][Driver][AMDGPU] Fix -mcode-object-version (#134230)

This patch updates flang to follow clang's behavior when processing the
`-mcode-object-version` option.

It is now used to populate an LLVM module flag called
`amdhsa_code_object_version` expected by the backend and also updates
the driver to add the `--amdhsa-code-object-version` option to the
frontend invocation for device compilation of AMDGPU targets.
This commit is contained in:
Sergio Afonso 2025-04-04 11:54:49 +01:00 committed by GitHub
parent a9a7b711e4
commit a17d49687a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 44 additions and 1 deletions

View File

@ -420,6 +420,9 @@ void Flang::AddAMDGPUTargetArgs(const ArgList &Args,
if (Arg *A = Args.getLastArg(options::OPT_mcode_object_version_EQ)) {
StringRef Val = A->getValue();
CmdArgs.push_back(Args.MakeArgString("-mcode-object-version=" + Val));
CmdArgs.push_back(Args.MakeArgString("-mllvm"));
CmdArgs.push_back(
Args.MakeArgString("--amdhsa-code-object-version=" + Val));
}
const ToolChain &TC = getToolChain();

View File

@ -95,7 +95,7 @@ public:
/// \brief Code object version for AMDGPU.
llvm::CodeObjectVersionKind CodeObjectVersion =
llvm::CodeObjectVersionKind::COV_5;
llvm::CodeObjectVersionKind::COV_None;
/// Optimization remark with an optional regular expression pattern.
struct OptRemark {

View File

@ -804,6 +804,17 @@ void CodeGenAction::generateLLVMIR() {
llvmModule->addModuleFlag(
llvm::Module::Error, "target-abi",
llvm::MDString::get(llvmModule->getContext(), targetOpts.abi));
if (triple.isAMDGPU() ||
(triple.isSPIRV() && triple.getVendor() == llvm::Triple::AMD)) {
// Emit amdhsa_code_object_version module flag, which is code object version
// times 100.
if (opts.CodeObjectVersion != llvm::CodeObjectVersionKind::COV_None) {
llvmModule->addModuleFlag(llvm::Module::Error,
"amdhsa_code_object_version",
opts.CodeObjectVersion);
}
}
}
static std::unique_ptr<llvm::raw_pwrite_stream>

View File

@ -5,5 +5,12 @@
! RUN: %flang -target x86_64-unknown-linux-gnu -mcode-object-version=3 -S %s -o \
! RUN: /dev/null 2>&1 | FileCheck --check-prefix=UNUSED_PARAM %s
! RUN: %flang -target amdgcn-amd-amdhsa -mcpu=gfx908 -mcode-object-version=5 -nogpulib -c %s -### 2>&1 \
! RUN: | FileCheck %s -check-prefix=VALID_USE
! INVALID_VERSION: error: invalid integral value '3' in '-mcode-object-version=3'
! UNUSED_PARAM: warning: argument unused during compilation: '-mcode-object-version=3' [-Wunused-command-line-argument]
! VALID_USE: "-fc1" "-triple" "amdgcn-amd-amdhsa"
! VALID_USE-SAME: "-mcode-object-version=5"
! VALID_USE-SAME: "-mllvm" "--amdhsa-code-object-version=5"

View File

@ -0,0 +1,22 @@
!REQUIRES: amdgpu-registered-target
!RUN: %flang_fc1 -emit-llvm -triple amdgcn-amd-amdhsa -target-cpu gfx908 %s -o - | FileCheck --check-prefix=COV-DEFAULT %s
!RUN: %flang_fc1 -emit-llvm -triple amdgcn-amd-amdhsa -target-cpu gfx908 -mcode-object-version=none %s -o - | FileCheck --check-prefix=COV-NONE %s
!RUN: %flang_fc1 -emit-llvm -triple amdgcn-amd-amdhsa -target-cpu gfx908 -mcode-object-version=4 %s -o - | FileCheck --check-prefix=COV-4 %s
!RUN: %flang_fc1 -emit-llvm -triple amdgcn-amd-amdhsa -target-cpu gfx908 -mcode-object-version=5 %s -o - | FileCheck --check-prefix=COV-5 %s
!RUN: %flang_fc1 -emit-llvm -triple amdgcn-amd-amdhsa -target-cpu gfx908 -mcode-object-version=6 %s -o - | FileCheck --check-prefix=COV-6 %s
!COV-DEFAULT-NOT: !{{.*}} = !{{{.*}}, !"amdhsa_code_object_version", {{.*}}}
!COV-NONE-NOT: !{{.*}} = !{{{.*}}, !"amdhsa_code_object_version", {{.*}}}
!COV-4: !llvm.module.flags = !{{{.*}}, ![[COV_FLAG:.*]]}
!COV-4: ![[COV_FLAG]] = !{i32 1, !"amdhsa_code_object_version", i32 400}
!COV-5: !llvm.module.flags = !{{{.*}}, ![[COV_FLAG:.*]]}
!COV-5: ![[COV_FLAG]] = !{i32 1, !"amdhsa_code_object_version", i32 500}
!COV-6: !llvm.module.flags = !{{{.*}}, ![[COV_FLAG:.*]]}
!COV-6: ![[COV_FLAG]] = !{i32 1, !"amdhsa_code_object_version", i32 600}
subroutine target_simple
end subroutine