[llvm] [llc] set canonical triple in module (PR #203725)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 20 10:15:19 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-x86
Author: Alex MacLean (AlexMaclean)
<details>
<summary>Changes</summary>
Prior to this change the effective triple for the TargetMachine and the triple in the module were determined by different criteria and diverged in some cases. This can create lots of issues in passes which check the module triple.
TargetMachine:
1. -march
2. -mtriple
3. target triple in incoming module
4. default target triple
Module:
1. -mtriple
2. target triple in incoming module
Stamp the canonical target triple into the module so that this divergence is no longer possible.
---
Full diff: https://github.com/llvm/llvm-project/pull/203725.diff
4 Files Affected:
- (removed) llvm/test/CodeGen/X86/expand-frem-no-libcall.ll (-31)
- (added) llvm/test/tools/llc/module-triple-default.ll (+14)
- (added) llvm/test/tools/llc/module-triple.ll (+16)
- (modified) llvm/tools/llc/llc.cpp (+2-2)
``````````diff
diff --git a/llvm/test/CodeGen/X86/expand-frem-no-libcall.ll b/llvm/test/CodeGen/X86/expand-frem-no-libcall.ll
deleted file mode 100644
index c29b072d96e8e..0000000000000
--- a/llvm/test/CodeGen/X86/expand-frem-no-libcall.ll
+++ /dev/null
@@ -1,31 +0,0 @@
-; This test underhandedly exploits a bug in llc's handling of -march.
-; The module ends up with no triple, and thus treated as unknown arch
-; with no library functions.
-
-; RUN: llc -march=x86-64 -stop-after=expand-ir-insts %s -o - | FileCheck --check-prefix=EXPAND %s
-; RUN: llc -mtriple=x86_64-linux-gnu -stop-after=expand-ir-insts %s -o - | FileCheck --check-prefix=LIBCALL %s
-
-; When the fmod libcall is unavailable, expand-ir-insts must expand
-; frem inline instead of leaving it for the DAG legalizer.
-
-; EXPAND-LABEL: define float @frem_f32
-; EXPAND-NOT: frem float
-; EXPAND: fmul float
-
-; LIBCALL-LABEL: define float @frem_f32
-; LIBCALL: frem float
-define float @frem_f32(float %a, float %b) {
- %r = frem float %a, %b
- ret float %r
-}
-
-; EXPAND-LABEL: define double @frem_f64
-; EXPAND-NOT: frem double
-; EXPAND: fmul double
-
-; LIBCALL-LABEL: define double @frem_f64
-; LIBCALL: frem double
-define double @frem_f64(double %a, double %b) {
- %r = frem double %a, %b
- ret double %r
-}
diff --git a/llvm/test/tools/llc/module-triple-default.ll b/llvm/test/tools/llc/module-triple-default.ll
new file mode 100644
index 0000000000000..161d76557450a
--- /dev/null
+++ b/llvm/test/tools/llc/module-triple-default.ll
@@ -0,0 +1,14 @@
+; REQUIRES: aarch64-registered-target
+; REQUIRES: default_triple
+
+;; Verify that llc correctly sets the module triple when one is not present.
+
+; RUN: llc -march=aarch64 -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=MARCH
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=MTRIPLE
+; RUN: llc -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=DEFAULT
+
+; MARCH: target triple = "aarch64-{{.*}}-{{.*}}"
+; MTRIPLE: target triple = "aarch64-unknown-linux-gnu"
+; DEFAULT: target triple = "{{.*}}-{{.*}}-{{.*}}"
+
+define void @f() { ret void }
diff --git a/llvm/test/tools/llc/module-triple.ll b/llvm/test/tools/llc/module-triple.ll
new file mode 100644
index 0000000000000..6e26babbda080
--- /dev/null
+++ b/llvm/test/tools/llc/module-triple.ll
@@ -0,0 +1,16 @@
+; REQUIRES: aarch64-registered-target
+; REQUIRES: x86-registered-target
+
+;; Verify that llc correctly sets the module triple when one is present.
+
+; RUN: llc -march=aarch64 -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=MARCH
+; RUN: llc -mtriple=aarch64-unknown-unknown -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=MTRIPLE
+; RUN: llc -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=MODULE
+
+; MARCH: target triple = "aarch64-unknown-linux-gnu"
+; MTRIPLE: target triple = "aarch64-unknown-unknown"
+; MODULE: target triple = "x86_64-unknown-linux-gnu"
+
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @f() { ret void }
diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp
index 4d00e0fcb048a..2c2a543862a28 100644
--- a/llvm/tools/llc/llc.cpp
+++ b/llvm/tools/llc/llc.cpp
@@ -675,8 +675,8 @@ static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &PluginList,
Err.print(argv[0], WithColor::error(errs(), argv[0]));
return 1;
}
- if (!TargetTriple.empty())
- M->setTargetTriple(Triple(Triple::normalize(TargetTriple)));
+
+ M->setTargetTriple(TheTriple);
std::optional<CodeModel::Model> CM_IR = M->getCodeModel();
if (!CM && CM_IR)
``````````
</details>
https://github.com/llvm/llvm-project/pull/203725
More information about the llvm-commits
mailing list