Skip to content

Commit 46edc02

Browse files
authored
[LoongArch] Adjust LA64 data layout by using n32:64 in layout string (#93814)
Although i32 type is illegal in the backend, LA64 has pretty good support for i32 types by using W instructions. By adding n32 to the DataLayout string, middle end optimizations will consider i32 to be a native type. One known effect of this is enabling LoopStrengthReduce on loops with i32 induction variables. This can be beneficial because C/C++ code often has loops with i32 induction variables due to the use of `int` or `unsigned int`. If this patch exposes performance issues, those are better addressed by tuning LSR or other passes.
1 parent f6c1e65 commit 46edc02

File tree

5 files changed

+13
-4
lines changed

5 files changed

+13
-4
lines changed

clang/lib/Basic/Targets/LoongArch.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArch64TargetInfo
133133
LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
134134
IntMaxType = Int64Type = SignedLong;
135135
HasUnalignedAccess = true;
136-
resetDataLayout("e-m:e-p:64:64-i64:64-i128:128-n64-S128");
136+
resetDataLayout("e-m:e-p:64:64-i64:64-i128:128-n32:64-S128");
137137
// TODO: select appropriate ABI.
138138
setABI("lp64d");
139139
}

llvm/docs/ReleaseNotes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ Changes to the Hexagon Backend
116116
Changes to the LoongArch Backend
117117
--------------------------------
118118

119+
* i32 is now a native type in the datalayout string. This enables
120+
LoopStrengthReduce for loops with i32 induction variables, among other
121+
optimizations.
122+
119123
Changes to the MIPS Backend
120124
---------------------------
121125

llvm/lib/IR/AutoUpgrade.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5368,8 +5368,8 @@ std::string llvm::UpgradeDataLayoutString(StringRef DL, StringRef TT) {
53685368
return DL.empty() ? std::string("G1") : (DL + "-G1").str();
53695369
}
53705370

5371-
if (T.isRISCV64()) {
5372-
// Make i32 a native type for 64-bit RISC-V.
5371+
if (T.isLoongArch64() || T.isRISCV64()) {
5372+
// Make i32 a native type for 64-bit LoongArch and RISC-V.
53735373
auto I = DL.find("-n64-");
53745374
if (I != StringRef::npos)
53755375
return (DL.take_front(I) + "-n32:64-" + DL.drop_front(I + 5)).str();

llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static cl::opt<bool>
4646

4747
static std::string computeDataLayout(const Triple &TT) {
4848
if (TT.isArch64Bit())
49-
return "e-m:e-p:64:64-i64:64-i128:128-n64-S128";
49+
return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
5050
assert(TT.isArch32Bit() && "only LA32 and LA64 are currently supported");
5151
return "e-m:e-p:32:32-i64:64-n32-S128";
5252
}

llvm/unittests/Bitcode/DataLayoutUpgradeTest.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ TEST(DataLayoutUpgradeTest, ValidDataLayoutUpgrade) {
6363
"riscv64"),
6464
"e-m:e-p:64:64-i64:64-i128:128-n32:64-S128");
6565

66+
// Check that LoongArch64 upgrades -n64 to -n32:64.
67+
EXPECT_EQ(UpgradeDataLayoutString("e-m:e-p:64:64-i64:64-i128:128-n64-S128",
68+
"loongarch64"),
69+
"e-m:e-p:64:64-i64:64-i128:128-n32:64-S128");
70+
6671
// Check that SPIR && SPIRV targets add -G1 if it's not present.
6772
EXPECT_EQ(UpgradeDataLayoutString("e-p:32:32", "spir"), "e-p:32:32-G1");
6873
EXPECT_EQ(UpgradeDataLayoutString("e-p:32:32", "spir64"), "e-p:32:32-G1");

0 commit comments

Comments
 (0)