Skip to content

Commit fc6e91f

Browse files
committed
[Local] Handle size mismatch between pointer/int in copyRangeMetadata()
SROA may convert a wide integer load into a narrow pointer load, make sure we don't crash. It would not be legal to transfer the metadata in this case.
1 parent bd66de5 commit fc6e91f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

llvm/lib/Transforms/Utils/Local.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2948,7 +2948,8 @@ void llvm::copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI,
29482948
return;
29492949

29502950
unsigned BitWidth = DL.getPointerTypeSizeInBits(NewTy);
2951-
if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) {
2951+
if (BitWidth == OldLI.getType()->getScalarSizeInBits() &&
2952+
!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) {
29522953
MDNode *NN = MDNode::get(OldLI.getContext(), std::nullopt);
29532954
NewLI.setMetadata(LLVMContext::MD_nonnull, NN);
29542955
}

llvm/test/Transforms/SROA/preserve-metadata.ll

+18
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ entry:
128128
ret ptr %load
129129
}
130130

131+
define i128 @load_i128_to_load_ptr() {
132+
; CHECK-LABEL: @load_i128_to_load_ptr(
133+
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint ptr null to i64
134+
; CHECK-NEXT: [[A_SROA_2_0_INSERT_EXT:%.*]] = zext i64 undef to i128
135+
; CHECK-NEXT: [[A_SROA_2_0_INSERT_SHIFT:%.*]] = shl i128 [[A_SROA_2_0_INSERT_EXT]], 64
136+
; CHECK-NEXT: [[A_SROA_2_0_INSERT_MASK:%.*]] = and i128 undef, 18446744073709551615
137+
; CHECK-NEXT: [[A_SROA_2_0_INSERT_INSERT:%.*]] = or i128 [[A_SROA_2_0_INSERT_MASK]], [[A_SROA_2_0_INSERT_SHIFT]]
138+
; CHECK-NEXT: [[A_SROA_0_0_INSERT_EXT:%.*]] = zext i64 [[TMP1]] to i128
139+
; CHECK-NEXT: [[A_SROA_0_0_INSERT_MASK:%.*]] = and i128 [[A_SROA_2_0_INSERT_INSERT]], -18446744073709551616
140+
; CHECK-NEXT: [[A_SROA_0_0_INSERT_INSERT:%.*]] = or i128 [[A_SROA_0_0_INSERT_MASK]], [[A_SROA_0_0_INSERT_EXT]]
141+
; CHECK-NEXT: ret i128 [[A_SROA_0_0_INSERT_INSERT]]
142+
;
143+
%a = alloca i128
144+
store ptr null, ptr %a
145+
%v = load i128, ptr %a, !range !{i128 1, i128 0}
146+
ret i128 %v
147+
}
148+
131149
!0 = !{}
132150
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
133151
; CHECK-MODIFY-CFG: {{.*}}

0 commit comments

Comments
 (0)