Skip to content

Commit

Permalink
[WebAssembly] Add -i128:128 to the datalayout string.
Browse files Browse the repository at this point in the history
Clang [defaults to aligning `__int128_t` to 16 bytes], while LLVM
`datalayout` strings [default to aligning `i128` to 8 bytes]. Wasm is
currently using the defaults for both, so it's inconsistent. Fix this
by adding `-i128:128` to Wasm's `datalayout` string so that it aligns
`i28` to 16 bytes too.

This is similar to dbad963 for SPARC.

This fixes rust-lang/rust#133991; see that issue for further discussion.

[defaults to aligning `__int128_t` to 16 bytes]: https://github.com/llvm/llvm-project/blob/f8b4182f076f8fe55f9d5f617b5a25008a77b22f/clang/lib/Basic/TargetInfo.cpp#L77
[default to aligning `i128` to 8 bytes]: https://llvm.org/docs/LangRef.html#langref-datalayout
  • Loading branch information
sunfishcode committed Dec 9, 2024
1 parent 00b07aa commit 337b104
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
18 changes: 10 additions & 8 deletions clang/lib/Basic/Targets/WebAssembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,12 @@ class LLVM_LIBRARY_VISIBILITY WebAssembly32TargetInfo
const TargetOptions &Opts)
: WebAssemblyTargetInfo(T, Opts) {
if (T.isOSEmscripten())
resetDataLayout("e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-f128:64-n32:64-"
"S128-ni:1:10:20");
else
resetDataLayout(
"e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20");
"e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-f128:64-n32:64-"
"S128-ni:1:10:20");
else
resetDataLayout("e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-"
"S128-ni:1:10:20");
}

protected:
Expand All @@ -207,11 +208,12 @@ class LLVM_LIBRARY_VISIBILITY WebAssembly64TargetInfo
PtrDiffType = SignedLong;
IntPtrType = SignedLong;
if (T.isOSEmscripten())
resetDataLayout("e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-f128:64-n32:64-"
"S128-ni:1:10:20");
else
resetDataLayout(
"e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20");
"e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-i128:128-f128:64-n32:64-"
"S128-ni:1:10:20");
else
resetDataLayout("e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-"
"S128-ni:1:10:20");
}

protected:
Expand Down
4 changes: 2 additions & 2 deletions clang/test/CodeGen/target-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@

// RUN: %clang_cc1 -triple wasm32-unknown-unknown -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=WEBASSEMBLY32
// WEBASSEMBLY32: target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
// WEBASSEMBLY32: target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20"

// RUN: %clang_cc1 -triple wasm64-unknown-unknown -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=WEBASSEMBLY64
// WEBASSEMBLY64: target datalayout = "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
// WEBASSEMBLY64: target datalayout = "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20"

// RUN: %clang_cc1 -triple lanai-unknown-unknown -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=LANAI
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/AutoUpgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5559,7 +5559,7 @@ std::string llvm::UpgradeDataLayoutString(StringRef DL, StringRef TT) {
return Res;
}

if (T.isSPARC() || (T.isMIPS64() && !DL.contains("m:m"))) {
if (T.isSPARC() || (T.isMIPS64() && !DL.contains("m:m")) || T.isWasm()) {
// Mips64 with o32 ABI did not add "-i128:128".
// Add "-i128:128"
std::string I64 = "-i64:64";
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ WebAssemblyTargetMachine::WebAssemblyTargetMachine(
T,
TT.isArch64Bit()
? (TT.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
"f128:64-n32:64-S128-ni:1:10:20"
"i128:128-f128:64-n32:64-S128-ni:1:10:20"
: "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
"n32:64-S128-ni:1:10:20")
"i128:128-n32:64-S128-ni:1:10:20")
: (TT.isOSEmscripten() ? "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
"f128:64-n32:64-S128-ni:1:10:20"
"i128:128-f128:64-n32:64-S128-ni:1:10:20"
: "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
"n32:64-S128-ni:1:10:20"),
"i128:128-n32:64-S128-ni:1:10:20"),
TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT),
getEffectiveCodeModel(CM, CodeModel::Large), OL),
TLOF(new WebAssemblyTargetObjectFile()),
Expand Down
26 changes: 26 additions & 0 deletions llvm/test/CodeGen/WebAssembly/data-align.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; RUN: llc < %s -march=wasm32 | FileCheck %s
; RUN: llc < %s -march=wasm64 | 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
12 changes: 12 additions & 0 deletions llvm/unittests/Bitcode/DataLayoutUpgradeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ TEST(DataLayoutUpgradeTest, ValidDataLayoutUpgrade) {
"e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64", "mips64el"),
"e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64");

// Check that WebAssembly targets add -i128:128.
EXPECT_EQ(
UpgradeDataLayoutString(
"e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20",
"wasm32"),
"e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20");
EXPECT_EQ(
UpgradeDataLayoutString(
"e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20",
"wasm64"),
"e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20");

// Check that SPIR && SPIRV targets add -G1 if it's not present.
EXPECT_EQ(UpgradeDataLayoutString("e-p:32:32", "spir"), "e-p:32:32-G1");
EXPECT_EQ(UpgradeDataLayoutString("e-p:32:32", "spir64"), "e-p:32:32-G1");
Expand Down

0 comments on commit 337b104

Please sign in to comment.