Skip to content

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

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 2 commits into from
Apr 4, 2025

Conversation

skatrak
Copy link
Member

@skatrak skatrak commented Apr 3, 2025

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 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.
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:AMDGPU clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category labels Apr 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 3, 2025

@llvm/pr-subscribers-clang-driver
@llvm/pr-subscribers-flang-driver
@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-clang

Author: Sergio Afonso (skatrak)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/134230.diff

5 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Flang.cpp (+3)
  • (modified) flang/include/flang/Frontend/CodeGenOptions.h (+1-1)
  • (modified) flang/lib/Frontend/FrontendActions.cpp (+11)
  • (modified) flang/test/Driver/code-object-version.f90 (+7)
  • (added) flang/test/Integration/amdgpu-code-object-version.f90 (+22)
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 8312234e33a64..82d5201e4f337 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -410,6 +410,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();
diff --git a/flang/include/flang/Frontend/CodeGenOptions.h b/flang/include/flang/Frontend/CodeGenOptions.h
index 23d99e1f0897a..2b4e823b3fef4 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.h
+++ b/flang/include/flang/Frontend/CodeGenOptions.h
@@ -95,7 +95,7 @@ class CodeGenOptions : public CodeGenOptionsBase {
 
   /// \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 {
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index bd2c0632cb35d..d304e74f34f5c 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -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>
diff --git a/flang/test/Driver/code-object-version.f90 b/flang/test/Driver/code-object-version.f90
index e10877563c4d0..430cc864d03ec 100644
--- a/flang/test/Driver/code-object-version.f90
+++ b/flang/test/Driver/code-object-version.f90
@@ -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"
diff --git a/flang/test/Integration/amdgpu-code-object-version.f90 b/flang/test/Integration/amdgpu-code-object-version.f90
new file mode 100644
index 0000000000000..c5194d2007f2a
--- /dev/null
+++ b/flang/test/Integration/amdgpu-code-object-version.f90
@@ -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

skatrak added a commit to skatrak/aomp that referenced this pull request Apr 3, 2025
This patch adds the `-L` lines in clang cfg files also to flang, making them
the same. This is currently needed, in addition to
llvm/llvm-project#134230, to get the
`-mcode-object-version` option to work properly for flang.
Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

@skatrak skatrak changed the title [Flang][OpenMP][Driver][AMDGPU] Fix -mcode-object-version [Flang][Driver][AMDGPU] Fix -mcode-object-version Apr 3, 2025
@skatrak skatrak merged commit a17d496 into llvm:main Apr 4, 2025
11 checks passed
@skatrak skatrak deleted the fix-mcode-object-version branch April 4, 2025 10:54
skatrak added a commit to ROCm/aomp that referenced this pull request Apr 4, 2025
This patch adds the `-L` lines in clang cfg files also to flang, making them
the same. This is currently needed, in addition to
llvm/llvm-project#134230, to get the
`-mcode-object-version` option to work properly for flang.
searlmc1 pushed a commit to ROCm/llvm-project that referenced this pull request Apr 4, 2025
breaks many smoke-fort, ovo, sollve tests

This reverts commit a17d496.
searlmc1 pushed a commit to ROCm/llvm-project that referenced this pull request Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AMDGPU clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category flang:driver flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants