Skip to content
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

[SPARC] Align i128 to 16 bytes in SPARC datalayouts #106951

Merged
merged 5 commits into from
Sep 30, 2024
Merged

Conversation

koachan
Copy link
Contributor

@koachan koachan commented Sep 2, 2024

Align i128s to 16 bytes, following the example at https://reviews.llvm.org/D86310.

clang already does this implicitly, but do it in backend code too for the benefit of other frontends (see e.g #102783 & rust-lang/rust#128950).

Align i128s to 16 bytes, following the example at https://reviews.llvm.org/D86310.

clang already does this, but do it in backend code too for the benefit of other
frontends (see e.g llvm#102783).
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:Sparc clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 2, 2024
@koachan
Copy link
Contributor Author

koachan commented Sep 2, 2024

CC @beetrees

@llvmbot
Copy link
Member

llvmbot commented Sep 2, 2024

@llvm/pr-subscribers-llvm-ir
@llvm/pr-subscribers-backend-sparc

@llvm/pr-subscribers-clang

Author: Koakuma (koachan)

Changes

Align i128s to 16 bytes, following the example at https://reviews.llvm.org/D86310.

clang already does this implicitly, but do it in backend code too for the benefit of other frontends (see e.g #102783 & rust-lang/rust#128950).


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

4 Files Affected:

  • (modified) clang/lib/Basic/Targets/Sparc.h (+3-3)
  • (modified) clang/test/CodeGen/target-data.c (+2-2)
  • (modified) llvm/lib/Target/Sparc/SparcTargetMachine.cpp (+4)
  • (added) llvm/test/CodeGen/SPARC/data-align.ll (+27)
diff --git a/clang/lib/Basic/Targets/Sparc.h b/clang/lib/Basic/Targets/Sparc.h
index 3357bee33e1ac7..ee0d3e2b4329eb 100644
--- a/clang/lib/Basic/Targets/Sparc.h
+++ b/clang/lib/Basic/Targets/Sparc.h
@@ -151,7 +151,7 @@ class LLVM_LIBRARY_VISIBILITY SparcV8TargetInfo : public SparcTargetInfo {
 public:
   SparcV8TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
       : SparcTargetInfo(Triple, Opts) {
-    resetDataLayout("E-m:e-p:32:32-i64:64-f128:64-n32-S64");
+    resetDataLayout("E-m:e-p:32:32-i64:64-i128:128-f128:64-n32-S64");
     // NetBSD / OpenBSD use long (same as llvm default); everyone else uses int.
     switch (getTriple().getOS()) {
     default:
@@ -188,7 +188,7 @@ class LLVM_LIBRARY_VISIBILITY SparcV8elTargetInfo : public SparcV8TargetInfo {
 public:
   SparcV8elTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
       : SparcV8TargetInfo(Triple, Opts) {
-    resetDataLayout("e-m:e-p:32:32-i64:64-f128:64-n32-S64");
+    resetDataLayout("e-m:e-p:32:32-i64:64-i128:128-f128:64-n32-S64");
   }
 };
 
@@ -198,7 +198,7 @@ class LLVM_LIBRARY_VISIBILITY SparcV9TargetInfo : public SparcTargetInfo {
   SparcV9TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
       : SparcTargetInfo(Triple, Opts) {
     // FIXME: Support Sparc quad-precision long double?
-    resetDataLayout("E-m:e-i64:64-n32:64-S128");
+    resetDataLayout("E-m:e-i64:64-i128:128-n32:64-S128");
     // This is an LP64 platform.
     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
 
diff --git a/clang/test/CodeGen/target-data.c b/clang/test/CodeGen/target-data.c
index 41cbd5a0219d5e..8548aa00cfe877 100644
--- a/clang/test/CodeGen/target-data.c
+++ b/clang/test/CodeGen/target-data.c
@@ -28,11 +28,11 @@
 
 // RUN: %clang_cc1 -triple sparc-sun-solaris -emit-llvm -o - %s | \
 // RUN:     FileCheck %s --check-prefix=SPARC-V8
-// SPARC-V8: target datalayout = "E-m:e-p:32:32-i64:64-f128:64-n32-S64"
+// SPARC-V8: target datalayout = "E-m:e-p:32:32-i64:64-i128:128-f128:64-n32-S64"
 
 // RUN: %clang_cc1 -triple sparcv9-sun-solaris -emit-llvm -o - %s | \
 // RUN: FileCheck %s --check-prefix=SPARC-V9
-// SPARC-V9: target datalayout = "E-m:e-i64:64-n32:64-S128"
+// SPARC-V9: target datalayout = "E-m:e-i64:64-i128:128-n32:64-S128"
 
 // RUN: %clang_cc1 -triple mipsel-linux-gnu -o - -emit-llvm %s |     \
 // RUN: FileCheck %s -check-prefix=MIPS-32EL
diff --git a/llvm/lib/Target/Sparc/SparcTargetMachine.cpp b/llvm/lib/Target/Sparc/SparcTargetMachine.cpp
index fec2d3a35ae6d2..50a96368bbdca9 100644
--- a/llvm/lib/Target/Sparc/SparcTargetMachine.cpp
+++ b/llvm/lib/Target/Sparc/SparcTargetMachine.cpp
@@ -48,6 +48,10 @@ static std::string computeDataLayout(const Triple &T, bool is64Bit) {
   // Alignments for 64 bit integers.
   Ret += "-i64:64";
 
+  // Alignments for 128 bit integers.
+  // This is not specified in the ABI document but is the de facto standard.
+  Ret += "-i128:128";
+
   // On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
   // On SparcV9 registers can hold 64 or 32 bits, on others only 32.
   if (is64Bit)
diff --git a/llvm/test/CodeGen/SPARC/data-align.ll b/llvm/test/CodeGen/SPARC/data-align.ll
new file mode 100644
index 00000000000000..d4a39524da44f6
--- /dev/null
+++ b/llvm/test/CodeGen/SPARC/data-align.ll
@@ -0,0 +1,27 @@
+; RUN: llc < %s -march=sparc | FileCheck %s
+; RUN: llc < %s -march=sparcel | FileCheck %s
+; RUN: llc < %s -march=sparcv9 | FileCheck %s
+
+; CHECK:      .Li8:
+; CHECK-DAG: .size .Li8, 1
+@i8 = private constant i8 42
+
+; CHECK:      .p2align 1
+; CHECK-NEXT: .Li16:
+; CHECK-DAG:  .size .Li16, 2
+@i16 = private constant i16 42
+
+; CHECK:      .p2align 2
+; CHECK-NEXT: .Li32:
+; CHECK-DAG:  .size .Li32, 4
+@i32 = private constant i32 42
+
+; CHECK:      .p2align 3
+; CHECK-NEXT: .Li64:
+; CHECK-DAG:  .size .Li64, 8
+@i64 = private constant i64 42
+
+; CHECK:      .p2align 4
+; CHECK-NEXT: .Li128:
+; CHECK-DAG:  .size .Li128, 16
+@i128 = private constant i128 42

@s-barannikov
Copy link
Contributor

I guess this is a breaking change?
Following the discussion in the linked revision it looks like this also needs some AutoUpgrade changes.

@nikic
Copy link
Contributor

nikic commented Sep 4, 2024

I guess this is a breaking change?

Yes, but as previously discussed, it's acceptable breakage.

Following the discussion in the linked revision it looks like this also needs some AutoUpgrade changes.

Yes, this should be handled in UpgradeDataLayoutString().

@koachan
Copy link
Contributor Author

koachan commented Sep 19, 2024

Ping?

std::string I128 = "-i128:128";
if (StringRef Ref = Res; !Ref.contains(I128)) {
SmallVector<StringRef, 4> Groups;
Regex R("^([Ee](-[mpi][^-]*)*)((-[^mpi][^-]*)*)$");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be easier to match for -i64:64- here? It looks like that part doesn't vary across subtargets, so we don't need more generic matching.

@brad0 brad0 requested a review from nikic September 29, 2024 02:04
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

// Add "-i128:128"
std::string I64 = "-i64:64";
std::string I128 = "-i128:128";
if (StringRef Ref = Res; !Ref.contains(I128)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (StringRef Ref = Res; !Ref.contains(I128)) {
if (!StringRef(Res).contains(I128)) {

@brad0
Copy link
Contributor

brad0 commented Sep 29, 2024

@s-barannikov

Copy link
Contributor

@s-barannikov s-barannikov left a comment

Choose a reason for hiding this comment

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

LGTM

@koachan koachan merged commit dbad963 into llvm:main Sep 30, 2024
6 of 8 checks passed
@koachan koachan deleted the sparc-i128 branch September 30, 2024 01:32
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 30, 2024

LLVM Buildbot has detected a new failure on builder clang-solaris11-sparcv9 running on solaris11-sparcv9 while building clang,llvm at step 5 "ninja check 1".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/13/builds/2592

Here is the relevant piece of the build log for the reference
Step 5 (ninja check 1) failure: stage 1 checked (failure)
******************** TEST 'LLVM :: BugPoint/metadata.ll' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 2: /opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/bin/bugpoint -load /opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/lib/BugpointPasses.so /opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/llvm/test/BugPoint/metadata.ll -output-prefix /opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/test/BugPoint/Output/metadata.ll.tmp -bugpoint-crashcalls -silence-passes -disable-namedmd-remove -disable-strip-debuginfo -disable-strip-debug-types > /dev/null
+ /opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/bin/bugpoint -load /opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/lib/BugpointPasses.so /opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/llvm/test/BugPoint/metadata.ll -output-prefix /opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/test/BugPoint/Output/metadata.ll.tmp -bugpoint-crashcalls -silence-passes -disable-namedmd-remove -disable-strip-debuginfo -disable-strip-debug-types
Instruction removal failed.  Sorry. :(  Please report a bug!

--

********************


@rorth
Copy link
Collaborator

rorth commented Sep 30, 2024

In a local sparcv9-sun-solaris2.11 build, I get (with -silence-passes removed)

> /var/llvm/dist-sparcv9-release-stage2-A-flang-clang19/tools/clang/stage2-bins/bin/bugpoint -load /var/llvm/dist-sparcv9-release-stage2-A-flang-clang19/tools/clang/stage2-bins/lib/BugpointPasses.so /vol/llvm/src/llvm-project/dist/llvm/test/BugPoint/remove_arguments_test.ll -output-prefix /var/llvm/dist-sparcv9-release-stage2-A-flang-clang19/tools/clang/stage2-bins/test/BugPoint/Output/remove_arguments_test.ll.tmp -bugpoint-crashcalls 
Read input file      : '/vol/llvm/src/llvm-project/dist/llvm/test/BugPoint/remove_arguments_test.ll'
*** All input ok
Running selected passes on program to test for crash: Assertion failed: Pos != size_t(-1) && "no i64 data layout found!", file /vol/llvm/src/llvm-project/dist/llvm/lib/IR/AutoUpgrade.cpp, line 5526
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /var/llvm/dist-sparcv9-release-stage2-A-flang-clang19/tools/clang/stage2-bins/bin/opt -bugpoint-enable-legacy-pm -disable-symbolication -o /var/llvm/dist-sparcv9-release-stage2-A-flang-clang19/tools/clang/stage2-bins/test/BugPoint/Output/remove_arguments_test.ll.tmp-output-919fb11.bc -load /var/llvm/dist-sparcv9-release-stage2-A-flang-clang19/tools/clang/stage2-bins/lib/BugpointPasses.so -bugpoint-crashcalls /var/llvm/dist-sparcv9-release-stage2-A-flang-clang19/tools/clang/stage2-bins/test/BugPoint/Output/remove_arguments_test.ll.tmp-input-7ed07c3.bc
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  opt       0x0000000107d31e74 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 36
1  opt       0x0000000107d32778 SignalHandler(int) + 896
2  libc.so.1 0x00007fffff0c6398 __sighndlr + 12
3  libc.so.1 0x00007fffff0b8c40 call_user_handler + 1024
4  libc.so.1 0x00007fffff0b9030 sigacthandler + 208
5  libc.so.1 0x00007fffff0cb4b0 __lwp_sigqueue + 8
6  libc.so.1 0x00007ffffefe519c abort + 252
7  libc.so.1 0x00007ffffefe5fd0 _assert + 96
8  opt       0x0000000107974b8c llvm::UpgradeDataLayoutString[abi:cxx11](llvm::StringRef, llvm::StringRef) + 4176
9  opt       0x00000001078c3ba4 (anonymous namespace)::BitcodeReader::parseModule(unsigned long, bool, llvm::ParserCallbacks)::$_0::operator()() const + 96
10 opt       0x00000001078bc3c8 (anonymous namespace)::BitcodeReader::parseModule(unsigned long, bool, llvm::ParserCallbacks) + 3804
11 opt       0x0000000107898b8c llvm::BitcodeModule::getModuleImpl(llvm::LLVMContext&, bool, bool, bool, llvm::ParserCallbacks) + 3592
12 opt       0x000000010789df6c llvm::BitcodeModule::parseModule(llvm::LLVMContext&, llvm::ParserCallbacks) + 296
13 opt       0x000000010789e184 llvm::parseBitcodeFile(llvm::MemoryBufferRef, llvm::LLVMContext&, llvm::ParserCallbacks) + 388
14 opt       0x0000000107710934 llvm::parseIR(llvm::MemoryBufferRef, llvm::SMDiagnostic&, llvm::LLVMContext&, llvm::ParserCallbacks) + 588
15 opt       0x0000000107711184 llvm::parseIRFile(llvm::StringRef, llvm::SMDiagnostic&, llvm::LLVMContext&, llvm::ParserCallbacks) + 540
16 opt       0x0000000103647560 optMain + 3140
17 opt       0x0000000103645f24 _start + 100
Crashed: Abort
Dumped core

and several more instances of that.

I wonder how this patch has ever been tested natively...

@rorth
Copy link
Collaborator

rorth commented Sep 30, 2024

FWIW, the same failures exist on Linux/sparc64, too.

@koachan
Copy link
Contributor Author

koachan commented Oct 1, 2024

My bad, yeah, I forgot to run bugpoint tests.
#110608 should fix it.

koachan added a commit that referenced this pull request Oct 2, 2024
It turns out that we cannot rely on the presence of `-i64:64` as a
position reference when adding the `-i128:128` datalayout string due to
some custom datalayout strings lacking it (e.g ones used by bugpoint,
among other things).
Do not add the `-i128:128` string in that case.

This fixes the regression introduced in
#106951.
xgupta pushed a commit to xgupta/llvm-project that referenced this pull request Oct 4, 2024
Align i128s to 16 bytes, following the example at
https://reviews.llvm.org/D86310.

clang already does this implicitly, but do it in backend code too for
the benefit of other frontends (see e.g
llvm#102783 &
rust-lang/rust#128950).
xgupta pushed a commit to xgupta/llvm-project that referenced this pull request Oct 4, 2024
It turns out that we cannot rely on the presence of `-i64:64` as a
position reference when adding the `-i128:128` datalayout string due to
some custom datalayout strings lacking it (e.g ones used by bugpoint,
among other things).
Do not add the `-i128:128` string in that case.

This fixes the regression introduced in
llvm#106951.
maurer added a commit to maurer/rust that referenced this pull request Oct 31, 2024
LLVM continues to align more 128-bit integers to 128-bits in the data
layout rather than relying on the high level language to do it. Update
SPARC target files to match and add a backcompat replacement for current
LLVMs.

See llvm/llvm-project#106951 for details
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Nov 1, 2024
llvm: Match new LLVM 128-bit integer alignment on sparc

LLVM continues to align more 128-bit integers to 128-bits in the data layout rather than relying on the high level language to do it. Update SPARC target files to match and add a backcompat replacement for current LLVMs.

See llvm/llvm-project#106951 for details

`@rustbot` label: +llvm-main

r? `@durin42`

(Please wait for the LLVM CI to come back before approving), creating this PR to get it tested there.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 1, 2024
Rollup merge of rust-lang#132422 - maurer:sparc-layout, r=durin42

llvm: Match new LLVM 128-bit integer alignment on sparc

LLVM continues to align more 128-bit integers to 128-bits in the data layout rather than relying on the high level language to do it. Update SPARC target files to match and add a backcompat replacement for current LLVMs.

See llvm/llvm-project#106951 for details

`@rustbot` label: +llvm-main

r? `@durin42`

(Please wait for the LLVM CI to come back before approving), creating this PR to get it tested there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:Sparc clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category llvm:ir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants