[llvm] fb7caa3 - [AsmPrinter] Reject ptrtoint to larger size in lowerConstant()
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 25 01:18:36 PDT 2022
Author: Nikita Popov
Date: 2022-07-25T10:18:27+02:00
New Revision: fb7caa3c7b53a4362139afe0158f297a891cc17b
URL: https://github.com/llvm/llvm-project/commit/fb7caa3c7b53a4362139afe0158f297a891cc17b
DIFF: https://github.com/llvm/llvm-project/commit/fb7caa3c7b53a4362139afe0158f297a891cc17b.diff
LOG: [AsmPrinter] Reject ptrtoint to larger size in lowerConstant()
When using a ptrtoint to a size larger than the pointer width in a
global initializer, we currently create a ptr & low_bit_mask style
MCExpr, which will later result in a relocation error during object
file emission.
This patch rejects the constant expression already during
lowerConstant(), which results in a much clearer error message
that references the constant expression at fault.
This fixes https://github.com/llvm/llvm-project/issues/56400,
for certain definitions of "fix".
Differential Revision: https://reviews.llvm.org/D130366
Added:
llvm/test/CodeGen/X86/ptrtoint-constexpr-invalid.ll
Modified:
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index e0050a47a6f6b..32a10ad41d1fd 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2795,12 +2795,7 @@ const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
DL.getTypeAllocSize(Op->getType()).getFixedSize())
return OpExpr;
- // Otherwise the pointer is smaller than the resultant integer, mask off
- // the high bits so we are sure to get a proper truncation if the input is
- // a constant expr.
- unsigned InBits = DL.getTypeAllocSizeInBits(Op->getType());
- const MCExpr *MaskExpr = MCConstantExpr::create(~0ULL >> (64-InBits), Ctx);
- return MCBinaryExpr::createAnd(OpExpr, MaskExpr, Ctx);
+ break; // Error
}
case Instruction::Sub: {
diff --git a/llvm/test/CodeGen/X86/ptrtoint-constexpr-invalid.ll b/llvm/test/CodeGen/X86/ptrtoint-constexpr-invalid.ll
new file mode 100644
index 0000000000000..f1f5d7e2b7204
--- /dev/null
+++ b/llvm/test/CodeGen/X86/ptrtoint-constexpr-invalid.ll
@@ -0,0 +1,10 @@
+; RUN: not --crash llc < %s -mtriple=i386-linux 2>&1 | FileCheck %s
+
+; ptrtoint expressions that cast to a wider integer type are not supported.
+; A frontend can achieve a similar result by casting to the correct integer
+; type and explicitly zeroing any additional bytes.
+; { i32, i32 } { i32 ptrtoint (ptr @r to i32), i32 0 }
+
+; CHECK: LLVM ERROR: Unsupported expression in static initializer: ptrtoint (ptr @r to i64)
+
+ at r = global i64 ptrtoint (ptr @r to i64)
diff --git a/llvm/test/CodeGen/X86/ptrtoint-constexpr.ll b/llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
index 15ad13022235a..58d1895f0b158 100644
--- a/llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
+++ b/llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
@@ -1,11 +1,11 @@
; RUN: llc < %s -mtriple=i386-linux | FileCheck %s
- %union.x = type { i64 }
+ %union.x = type { i32 }
; CHECK: .globl r
; CHECK: r:
-; CHECK: .quad r&4294967295
+; CHECK: .long r
- at r = global %union.x { i64 ptrtoint (ptr @r to i64) }, align 4
+ at r = global %union.x { i32 ptrtoint (ptr @r to i32) }, align 4
; CHECK: .globl x
; CHECK: x:
More information about the llvm-commits
mailing list