Skip to content

Commit eb73af4

Browse files
committed
[Attributor] Handle undef and null in AAAlignFloating
Both `undef` and `nullptr` are maximally aligned. This is especially important as we often see `undef` until a proper value has been identified during simplification.
1 parent ad26e19 commit eb73af4

File tree

6 files changed

+86
-10
lines changed

6 files changed

+86
-10
lines changed

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4593,13 +4593,16 @@ struct AAAlignFloating : AAAlignImpl {
45934593

45944594
auto VisitValueCB = [&](Value &V, const Instruction *,
45954595
AAAlign::StateType &T, bool Stripped) -> bool {
4596+
if (isa<UndefValue>(V) || isa<ConstantPointerNull>(V))
4597+
return true;
45964598
const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V),
45974599
DepClassTy::REQUIRED);
45984600
if (!Stripped && this == &AA) {
45994601
int64_t Offset;
46004602
unsigned Alignment = 1;
46014603
if (const Value *Base =
46024604
GetPointerBaseWithConstantOffset(&V, Offset, DL)) {
4605+
// TODO: Use AAAlign for the base too.
46034606
Align PA = Base->getPointerAlignment(DL);
46044607
// BasePointerAddr + Offset = Alignment * Q for some integer Q.
46054608
// So we can say that the maximum power of two which is a divisor of

llvm/test/Transforms/Attributor/ArgumentPromotion/variadic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ entry:
3535
; Function Attrs: nounwind uwtable
3636
define internal void @callee_t0f(i8* nocapture readnone %tp13, i8* nocapture readnone %tp14, i8* nocapture readnone %tp15, i8* nocapture readnone %tp16, i8* nocapture readnone %tp17, ...) {
3737
; CHECK-LABEL: define {{[^@]+}}@callee_t0f
38-
; CHECK-SAME: (i8* noalias nocapture nofree nonnull readnone [[TP13:%.*]], i8* noalias nocapture nofree nonnull readnone [[TP14:%.*]], i8* noalias nocapture nofree nonnull readnone [[TP15:%.*]], i8* noalias nocapture nofree nonnull readnone [[TP16:%.*]], i8* noalias nocapture nofree nonnull readnone [[TP17:%.*]], ...) {
38+
; CHECK-SAME: (i8* noalias nocapture nofree nonnull readnone align 4294967296 [[TP13:%.*]], i8* noalias nocapture nofree nonnull readnone align 4294967296 [[TP14:%.*]], i8* noalias nocapture nofree nonnull readnone align 4294967296 [[TP15:%.*]], i8* noalias nocapture nofree nonnull readnone align 4294967296 [[TP16:%.*]], i8* noalias nocapture nofree nonnull readnone align 4294967296 [[TP17:%.*]], ...) {
3939
; CHECK-NEXT: entry:
4040
; CHECK-NEXT: call void @sink(i32 noundef 0)
4141
; CHECK-NEXT: ret void

llvm/test/Transforms/Attributor/align.ll

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1616
; CHECK: @[[A1:[a-zA-Z0-9_$"\\.-]+]] = common global i8 0, align 8
1717
; CHECK: @[[A2:[a-zA-Z0-9_$"\\.-]+]] = common global i8 0, align 16
1818
; CHECK: @[[CND:[a-zA-Z0-9_$"\\.-]+]] = external global i1
19+
; CHECK: @[[G:[a-zA-Z0-9_$"\\.-]+]] = global i8 0, align 32
1920
;.
2021
define i32* @test1(i32* align 8 %0) #0 {
2122
; CHECK: Function Attrs: nofree noinline norecurse nosync nounwind readnone willreturn uwtable
@@ -1114,6 +1115,75 @@ define void @align4_caller(i8* %p) {
11141115

11151116
declare void @align4_callee(i8* align(4) %p)
11161117

1118+
@G = global i8 0, align 32
1119+
1120+
define internal i8* @aligned_8_return(i8* %a, i1 %c1, i1 %c2) norecurse {
1121+
; NOT_CGSCC_OPM: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
1122+
; NOT_CGSCC_OPM-LABEL: define {{[^@]+}}@aligned_8_return
1123+
; NOT_CGSCC_OPM-SAME: (i8* noalias nofree readnone align 16 "no-capture-maybe-returned" [[A:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) #[[ATTR9]] {
1124+
; NOT_CGSCC_OPM-NEXT: [[STACK:%.*]] = alloca i8*, align 8
1125+
; NOT_CGSCC_OPM-NEXT: br i1 [[C1]], label [[T:%.*]], label [[F:%.*]]
1126+
; NOT_CGSCC_OPM: t:
1127+
; NOT_CGSCC_OPM-NEXT: [[GEP:%.*]] = getelementptr i8, i8* @G, i32 8
1128+
; NOT_CGSCC_OPM-NEXT: [[SEL:%.*]] = select i1 [[C2]], i8* [[A]], i8* [[GEP]]
1129+
; NOT_CGSCC_OPM-NEXT: store i8* [[SEL]], i8** [[STACK]], align 8
1130+
; NOT_CGSCC_OPM-NEXT: br label [[END:%.*]]
1131+
; NOT_CGSCC_OPM: f:
1132+
; NOT_CGSCC_OPM-NEXT: store i8* @G, i8** [[STACK]], align 8
1133+
; NOT_CGSCC_OPM-NEXT: br label [[END]]
1134+
; NOT_CGSCC_OPM: end:
1135+
; NOT_CGSCC_OPM-NEXT: [[L:%.*]] = load i8*, i8** [[STACK]], align 8
1136+
; NOT_CGSCC_OPM-NEXT: ret i8* [[L]]
1137+
;
1138+
; IS__CGSCC_OPM: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
1139+
; IS__CGSCC_OPM-LABEL: define {{[^@]+}}@aligned_8_return
1140+
; IS__CGSCC_OPM-SAME: (i8* noalias nofree readnone align 16 "no-capture-maybe-returned" [[A:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) #[[ATTR10]] {
1141+
; IS__CGSCC_OPM-NEXT: [[STACK:%.*]] = alloca i8*, align 8
1142+
; IS__CGSCC_OPM-NEXT: br i1 [[C1]], label [[T:%.*]], label [[F:%.*]]
1143+
; IS__CGSCC_OPM: t:
1144+
; IS__CGSCC_OPM-NEXT: [[GEP:%.*]] = getelementptr i8, i8* @G, i32 8
1145+
; IS__CGSCC_OPM-NEXT: [[SEL:%.*]] = select i1 [[C2]], i8* [[A]], i8* [[GEP]]
1146+
; IS__CGSCC_OPM-NEXT: store i8* [[SEL]], i8** [[STACK]], align 8
1147+
; IS__CGSCC_OPM-NEXT: br label [[END:%.*]]
1148+
; IS__CGSCC_OPM: f:
1149+
; IS__CGSCC_OPM-NEXT: store i8* @G, i8** [[STACK]], align 8
1150+
; IS__CGSCC_OPM-NEXT: br label [[END]]
1151+
; IS__CGSCC_OPM: end:
1152+
; IS__CGSCC_OPM-NEXT: [[L:%.*]] = load i8*, i8** [[STACK]], align 8
1153+
; IS__CGSCC_OPM-NEXT: ret i8* [[L]]
1154+
;
1155+
%stack = alloca i8*
1156+
br i1 %c1, label %t, label %f
1157+
t:
1158+
%gep = getelementptr i8, i8* @G, i32 8
1159+
%sel = select i1 %c2, i8* %a, i8* %gep
1160+
store i8* %sel, i8** %stack
1161+
br label %end
1162+
f:
1163+
store i8* @G, i8** %stack
1164+
br label %end
1165+
end:
1166+
%l = load i8*, i8** %stack
1167+
ret i8* %l
1168+
}
1169+
1170+
define i8* @aligned_8_return_caller(i8* align(16) %a, i1 %c1, i1 %c2) {
1171+
; NOT_CGSCC_OPM: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
1172+
; NOT_CGSCC_OPM-LABEL: define {{[^@]+}}@aligned_8_return_caller
1173+
; NOT_CGSCC_OPM-SAME: (i8* nofree readnone align 16 "no-capture-maybe-returned" [[A:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) #[[ATTR9]] {
1174+
; NOT_CGSCC_OPM-NEXT: [[R:%.*]] = call i8* @aligned_8_return(i8* noalias nofree readnone align 16 "no-capture-maybe-returned" [[A]], i1 [[C1]], i1 [[C2]]) #[[ATTR12:[0-9]+]]
1175+
; NOT_CGSCC_OPM-NEXT: ret i8* [[R]]
1176+
;
1177+
; IS__CGSCC_OPM: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
1178+
; IS__CGSCC_OPM-LABEL: define {{[^@]+}}@aligned_8_return_caller
1179+
; IS__CGSCC_OPM-SAME: (i8* nofree readnone align 16 "no-capture-maybe-returned" [[A:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) #[[ATTR10]] {
1180+
; IS__CGSCC_OPM-NEXT: [[R:%.*]] = call i8* @aligned_8_return(i8* noalias nofree readnone align 16 "no-capture-maybe-returned" [[A]], i1 [[C1]], i1 [[C2]]) #[[ATTR13:[0-9]+]]
1181+
; IS__CGSCC_OPM-NEXT: ret i8* [[R]]
1182+
;
1183+
%r = call i8* @aligned_8_return(i8* %a, i1 %c1, i1 %c2)
1184+
ret i8* %r
1185+
}
1186+
11171187
attributes #0 = { nounwind uwtable noinline }
11181188
attributes #1 = { uwtable noinline }
11191189
attributes #2 = { null_pointer_is_valid }
@@ -1130,6 +1200,7 @@ attributes #2 = { null_pointer_is_valid }
11301200
; IS__TUNIT____: attributes #[[ATTR9]] = { nofree norecurse nosync nounwind readnone willreturn }
11311201
; IS__TUNIT____: attributes #[[ATTR10]] = { nofree norecurse nosync nounwind readonly willreturn }
11321202
; IS__TUNIT____: attributes #[[ATTR11]] = { nofree nosync nounwind readonly willreturn }
1203+
; IS__TUNIT____: attributes #[[ATTR12]] = { nofree nosync nounwind readnone willreturn }
11331204
;.
11341205
; IS__CGSCC_OPM: attributes #[[ATTR0]] = { nofree noinline norecurse nosync nounwind readnone willreturn uwtable }
11351206
; IS__CGSCC_OPM: attributes #[[ATTR1]] = { nofree noinline nosync nounwind readnone willreturn uwtable }
@@ -1144,6 +1215,7 @@ attributes #2 = { null_pointer_is_valid }
11441215
; IS__CGSCC_OPM: attributes #[[ATTR10]] = { nofree norecurse nosync nounwind readnone willreturn }
11451216
; IS__CGSCC_OPM: attributes #[[ATTR11]] = { nofree norecurse nosync nounwind readonly willreturn }
11461217
; IS__CGSCC_OPM: attributes #[[ATTR12]] = { readonly willreturn }
1218+
; IS__CGSCC_OPM: attributes #[[ATTR13]] = { readnone willreturn }
11471219
;.
11481220
; IS__CGSCC_NPM: attributes #[[ATTR0]] = { nofree noinline norecurse nosync nounwind readnone willreturn uwtable }
11491221
; IS__CGSCC_NPM: attributes #[[ATTR1]] = { noinline norecurse nounwind uwtable }
@@ -1157,4 +1229,5 @@ attributes #2 = { null_pointer_is_valid }
11571229
; IS__CGSCC_NPM: attributes #[[ATTR9]] = { nofree norecurse nosync nounwind readnone willreturn }
11581230
; IS__CGSCC_NPM: attributes #[[ATTR10]] = { nofree norecurse nosync nounwind readonly willreturn }
11591231
; IS__CGSCC_NPM: attributes #[[ATTR11]] = { readonly willreturn }
1232+
; IS__CGSCC_NPM: attributes #[[ATTR12]] = { readnone willreturn }
11601233
;.

llvm/test/Transforms/Attributor/value-simplify-gpu.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,11 @@ define internal void @level2a(i32* %addr) {
308308
;
309309
; IS__CGSCC_OPM: Function Attrs: norecurse nosync nounwind
310310
; IS__CGSCC_OPM-LABEL: define {{[^@]+}}@level2a
311-
; IS__CGSCC_OPM-SAME: (i32* noalias nocapture nofree nonnull readnone align 4 dereferenceable(4) [[ADDR:%.*]]) #[[ATTR1]] {
311+
; IS__CGSCC_OPM-SAME: (i32* noalias nocapture nofree nonnull readnone align 4294967296 dereferenceable(4) [[ADDR:%.*]]) #[[ATTR1]] {
312312
; IS__CGSCC_OPM-NEXT: entry:
313313
; IS__CGSCC_OPM-NEXT: [[TMP0:%.*]] = load i32, i32* addrspacecast (i32 addrspace(3)* @ReachableNonKernel to i32*), align 4
314314
; IS__CGSCC_OPM-NEXT: [[TMP1:%.*]] = load i32, i32* addrspacecast (i32 addrspace(3)* @UnreachableNonKernel to i32*), align 4
315-
; IS__CGSCC_OPM-NEXT: [[TMP2:%.*]] = load i32, i32* undef, align 4
315+
; IS__CGSCC_OPM-NEXT: [[TMP2:%.*]] = load i32, i32* undef, align 4294967296
316316
; IS__CGSCC_OPM-NEXT: call void @use(i32 [[TMP0]], i32 [[TMP1]], i32 17) #[[ATTR4]]
317317
; IS__CGSCC_OPM-NEXT: ret void
318318
;
@@ -347,11 +347,11 @@ define internal void @level2b(i32* %addr) {
347347
;
348348
; IS__CGSCC_OPM: Function Attrs: norecurse nosync nounwind
349349
; IS__CGSCC_OPM-LABEL: define {{[^@]+}}@level2b
350-
; IS__CGSCC_OPM-SAME: (i32* noalias nocapture nofree nonnull readnone align 4 dereferenceable(4) [[ADDR:%.*]]) #[[ATTR1]] {
350+
; IS__CGSCC_OPM-SAME: (i32* noalias nocapture nofree nonnull readnone align 4294967296 dereferenceable(4) [[ADDR:%.*]]) #[[ATTR1]] {
351351
; IS__CGSCC_OPM-NEXT: entry:
352352
; IS__CGSCC_OPM-NEXT: [[TMP0:%.*]] = load i32, i32* addrspacecast (i32 addrspace(3)* @ReachableNonKernel to i32*), align 4
353353
; IS__CGSCC_OPM-NEXT: [[TMP1:%.*]] = load i32, i32* addrspacecast (i32 addrspace(3)* @UnreachableNonKernel to i32*), align 4
354-
; IS__CGSCC_OPM-NEXT: [[TMP2:%.*]] = load i32, i32* undef, align 4
354+
; IS__CGSCC_OPM-NEXT: [[TMP2:%.*]] = load i32, i32* undef, align 4294967296
355355
; IS__CGSCC_OPM-NEXT: call void @use(i32 [[TMP0]], i32 [[TMP1]], i32 17) #[[ATTR4]]
356356
; IS__CGSCC_OPM-NEXT: ret void
357357
;

llvm/test/Transforms/Attributor/value-simplify.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ define internal void @callee_is_undef(void ()* %fn) {
916916
; IS__TUNIT____-NEXT: ret void
917917
;
918918
; IS__CGSCC____-LABEL: define {{[^@]+}}@callee_is_undef
919-
; IS__CGSCC____-SAME: (void ()* nocapture nofree noundef nonnull [[FN:%.*]]) {
919+
; IS__CGSCC____-SAME: (void ()* nocapture nofree noundef nonnull align 4294967296 [[FN:%.*]]) {
920920
; IS__CGSCC____-NEXT: call void [[FN]]()
921921
; IS__CGSCC____-NEXT: ret void
922922
;

llvm/test/Transforms/OpenMP/spmdization.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,25 +2169,25 @@ user_code.entry: ; preds = %entry
21692169
; Function Attrs: alwaysinline convergent nounwind
21702170
define internal void @.omp_outlined.(i32 %.global_tid., i32* noalias %.part_id., i8* noalias %.privates., void (i8*, ...)* noalias %.copy_fn., i8* %.task_t., %struct.anon* noalias %__context) #9 {
21712171
; AMDGPU-LABEL: define {{[^@]+}}@.omp_outlined.
2172-
; AMDGPU-SAME: (i32 [[DOTGLOBAL_TID_:%.*]], i32* noalias nocapture nofree nonnull readnone [[DOTPART_ID_:%.*]], i8* noalias nocapture nofree readnone align 4294967296 [[DOTPRIVATES_:%.*]], void (i8*, ...)* noalias nocapture nofree readnone [[DOTCOPY_FN_:%.*]], i8* noalias nocapture nofree nonnull readnone align 8 dereferenceable(8) [[DOTTASK_T_:%.*]], %struct.anon* noalias nocapture nofree readnone [[__CONTEXT:%.*]]) #[[ATTR0]] {
2172+
; AMDGPU-SAME: (i32 [[DOTGLOBAL_TID_:%.*]], i32* noalias nocapture nofree nonnull readnone [[DOTPART_ID_:%.*]], i8* noalias nocapture nofree readnone align 4294967296 [[DOTPRIVATES_:%.*]], void (i8*, ...)* noalias nocapture nofree readnone align 4294967296 [[DOTCOPY_FN_:%.*]], i8* noalias nocapture nofree nonnull readnone align 8 dereferenceable(8) [[DOTTASK_T_:%.*]], %struct.anon* noalias nocapture nofree readnone [[__CONTEXT:%.*]]) #[[ATTR0]] {
21732173
; AMDGPU-NEXT: entry:
21742174
; AMDGPU-NEXT: call void @spmd_amenable() #[[ATTR5]]
21752175
; AMDGPU-NEXT: ret void
21762176
;
21772177
; NVPTX-LABEL: define {{[^@]+}}@.omp_outlined.
2178-
; NVPTX-SAME: (i32 [[DOTGLOBAL_TID_:%.*]], i32* noalias nocapture nofree nonnull readnone [[DOTPART_ID_:%.*]], i8* noalias nocapture nofree readnone align 4294967296 [[DOTPRIVATES_:%.*]], void (i8*, ...)* noalias nocapture nofree readnone [[DOTCOPY_FN_:%.*]], i8* noalias nocapture nofree nonnull readnone align 8 dereferenceable(8) [[DOTTASK_T_:%.*]], %struct.anon* noalias nocapture nofree readnone [[__CONTEXT:%.*]]) #[[ATTR0]] {
2178+
; NVPTX-SAME: (i32 [[DOTGLOBAL_TID_:%.*]], i32* noalias nocapture nofree nonnull readnone [[DOTPART_ID_:%.*]], i8* noalias nocapture nofree readnone align 4294967296 [[DOTPRIVATES_:%.*]], void (i8*, ...)* noalias nocapture nofree readnone align 4294967296 [[DOTCOPY_FN_:%.*]], i8* noalias nocapture nofree nonnull readnone align 8 dereferenceable(8) [[DOTTASK_T_:%.*]], %struct.anon* noalias nocapture nofree readnone [[__CONTEXT:%.*]]) #[[ATTR0]] {
21792179
; NVPTX-NEXT: entry:
21802180
; NVPTX-NEXT: call void @spmd_amenable() #[[ATTR5]]
21812181
; NVPTX-NEXT: ret void
21822182
;
21832183
; AMDGPU-DISABLED-LABEL: define {{[^@]+}}@.omp_outlined.
2184-
; AMDGPU-DISABLED-SAME: (i32 [[DOTGLOBAL_TID_:%.*]], i32* noalias nocapture nofree nonnull readnone [[DOTPART_ID_:%.*]], i8* noalias nocapture nofree readnone align 4294967296 [[DOTPRIVATES_:%.*]], void (i8*, ...)* noalias nocapture nofree readnone [[DOTCOPY_FN_:%.*]], i8* noalias nocapture nofree nonnull readnone align 8 dereferenceable(8) [[DOTTASK_T_:%.*]], %struct.anon* noalias nocapture nofree readnone [[__CONTEXT:%.*]]) #[[ATTR0]] {
2184+
; AMDGPU-DISABLED-SAME: (i32 [[DOTGLOBAL_TID_:%.*]], i32* noalias nocapture nofree nonnull readnone [[DOTPART_ID_:%.*]], i8* noalias nocapture nofree readnone align 4294967296 [[DOTPRIVATES_:%.*]], void (i8*, ...)* noalias nocapture nofree readnone align 4294967296 [[DOTCOPY_FN_:%.*]], i8* noalias nocapture nofree nonnull readnone align 8 dereferenceable(8) [[DOTTASK_T_:%.*]], %struct.anon* noalias nocapture nofree readnone [[__CONTEXT:%.*]]) #[[ATTR0]] {
21852185
; AMDGPU-DISABLED-NEXT: entry:
21862186
; AMDGPU-DISABLED-NEXT: call void @spmd_amenable() #[[ATTR5]]
21872187
; AMDGPU-DISABLED-NEXT: ret void
21882188
;
21892189
; NVPTX-DISABLED-LABEL: define {{[^@]+}}@.omp_outlined.
2190-
; NVPTX-DISABLED-SAME: (i32 [[DOTGLOBAL_TID_:%.*]], i32* noalias nocapture nofree nonnull readnone [[DOTPART_ID_:%.*]], i8* noalias nocapture nofree readnone align 4294967296 [[DOTPRIVATES_:%.*]], void (i8*, ...)* noalias nocapture nofree readnone [[DOTCOPY_FN_:%.*]], i8* noalias nocapture nofree nonnull readnone align 8 dereferenceable(8) [[DOTTASK_T_:%.*]], %struct.anon* noalias nocapture nofree readnone [[__CONTEXT:%.*]]) #[[ATTR0]] {
2190+
; NVPTX-DISABLED-SAME: (i32 [[DOTGLOBAL_TID_:%.*]], i32* noalias nocapture nofree nonnull readnone [[DOTPART_ID_:%.*]], i8* noalias nocapture nofree readnone align 4294967296 [[DOTPRIVATES_:%.*]], void (i8*, ...)* noalias nocapture nofree readnone align 4294967296 [[DOTCOPY_FN_:%.*]], i8* noalias nocapture nofree nonnull readnone align 8 dereferenceable(8) [[DOTTASK_T_:%.*]], %struct.anon* noalias nocapture nofree readnone [[__CONTEXT:%.*]]) #[[ATTR0]] {
21912191
; NVPTX-DISABLED-NEXT: entry:
21922192
; NVPTX-DISABLED-NEXT: call void @spmd_amenable() #[[ATTR5]]
21932193
; NVPTX-DISABLED-NEXT: ret void

0 commit comments

Comments
 (0)