[PATCH] D130366: [AsmPrinter] Reject ptrtoint to larger size in lowerConstant()

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 22 08:03:32 PDT 2022


nikic created this revision.
nikic added a reviewer: MaskRay.
Herald added subscribers: jsji, StephenFan, pengfei, hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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".


https://reviews.llvm.org/D130366

Files:
  llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  llvm/test/CodeGen/X86/ptrtoint-constexpr-invalid.ll
  llvm/test/CodeGen/X86/ptrtoint-constexpr.ll


Index: llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
===================================================================
--- llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
+++ 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:
Index: llvm/test/CodeGen/X86/ptrtoint-constexpr-invalid.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/ptrtoint-constexpr-invalid.ll
@@ -0,0 +1,5 @@
+; RUN: not --crash llc < %s -mtriple=i386-linux 2>&1 | FileCheck %s
+
+; CHECK: LLVM ERROR: Unsupported expression in static initializer: ptrtoint (ptr @r to i64)
+
+ at r = global i64 ptrtoint (ptr @r to i64)
Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2795,12 +2795,7 @@
         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: {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130366.446838.patch
Type: text/x-patch
Size: 1767 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220722/f1ac08a1/attachment.bin>


More information about the llvm-commits mailing list