[PATCH] D12946: Avoid pointer truncation by InstCombine with IntToPtr combining
Stanislav Mekhanoshin via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 17 15:53:41 PDT 2015
rampitec created this revision.
rampitec added reviewers: arsenm, joker.eph.
rampitec added a subscriber: llvm-commits.
rampitec set the repository for this revision to rL LLVM.
InstCombiner::visitIntToPtr can insert a zero extend or truncate to match pointer size. The pointer size is determined by a corresponding DataLayout record for a given address space.
The problem occurs when that is a pointer to a function. There are no DataLayout fields to represent code, so size of a pointer to code is unknown.
Problem manifest itself as a pointer truncation in case if target is 64 bit, but a pointer to address space zero is 32 bit (for example "e-p:32:64-p1:64:64"). In this situation InstCombine would produce from:
%0 = ptrtoint i8 addrspace(1)* %ptr to i64
%1 = inttoptr i64 %0 to i32 ()*
%call = tail call spir_func i32 %1()
a malformed call to a function using truncated pointer:
%0 = ptrtoint i8 addrspace(1)* %ptr to i64
%1 = trunc i64 %0 to i32
%2 = inttoptr i32 %1 to i32 ()*
%call = tail call spir_func i32 %2()
In a longer term that is needed to create a mechanism to provide code pointer size info. Currently patch disables this optimization on a function pointer.
Repository:
rL LLVM
http://reviews.llvm.org/D12946
Files:
lib/Transforms/InstCombine/InstCombineCasts.cpp
test/Transforms/InstCombine/combine-func-ptr.ll
Index: test/Transforms/InstCombine/combine-func-ptr.ll
===================================================================
--- test/Transforms/InstCombine/combine-func-ptr.ll
+++ test/Transforms/InstCombine/combine-func-ptr.ll
@@ -0,0 +1,16 @@
+; RUN: opt -O3 -S %s | not FileCheck %s
+;
+; The 64 bit function pointer should not be truncated even with 32 bit pointer size in address space 0
+;
+; CHECK: trunc
+
+target datalayout = "e-p:32:64-p1:64:64"
+
+define spir_kernel void @combine_func_ptr(i8 addrspace(1)* %ptr) {
+entry:
+ %0 = ptrtoint i8 addrspace(1)* %ptr to i64
+ %1 = inttoptr i64 %0 to i32 ()*
+ %call = tail call spir_func i32 %1()
+ ret void
+}
+
Index: lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1447,7 +1447,9 @@
// cast to be exposed to other transforms.
unsigned AS = CI.getAddressSpace();
if (CI.getOperand(0)->getType()->getScalarSizeInBits() !=
- DL.getPointerSizeInBits(AS)) {
+ DL.getPointerSizeInBits(AS) &&
+ !CI.getType()->getPointerElementType()->isFunctionTy()) {
+ // The size of a pointer to function is not defined by any DataLayout field
Type *Ty = DL.getIntPtrType(CI.getContext(), AS);
if (CI.getType()->isVectorTy()) // Handle vectors of pointers.
Ty = VectorType::get(Ty, CI.getType()->getVectorNumElements());
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12946.35041.patch
Type: text/x-patch
Size: 1482 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150917/d225d849/attachment.bin>
More information about the llvm-commits
mailing list