Skip to content

Commit 696efd7

Browse files
committed
[IR][Attributes] Take "best" from Callbase/Call Function when getting attributes
We where missing this for: param: `derefereneacble` param: `derefereneacble_or_null` param/ret: `align` ret: `range` ret: `noalias`
1 parent 170dab9 commit 696efd7

File tree

6 files changed

+69
-42
lines changed

6 files changed

+69
-42
lines changed

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,18 +1766,10 @@ class CallBase : public Instruction {
17661766
}
17671767

17681768
/// Extract the alignment of the return value.
1769-
MaybeAlign getRetAlign() const {
1770-
if (auto Align = Attrs.getRetAlignment())
1771-
return Align;
1772-
if (const Function *F = getCalledFunction())
1773-
return F->getAttributes().getRetAlignment();
1774-
return std::nullopt;
1775-
}
1769+
MaybeAlign getRetAlign() const;
17761770

17771771
/// Extract the alignment for a call or parameter (0=unknown).
1778-
MaybeAlign getParamAlign(unsigned ArgNo) const {
1779-
return Attrs.getParamAlignment(ArgNo);
1780-
}
1772+
MaybeAlign getParamAlign(unsigned ArgNo) const;
17811773

17821774
MaybeAlign getParamStackAlign(unsigned ArgNo) const {
17831775
return Attrs.getParamStackAlignment(ArgNo);
@@ -1847,7 +1839,11 @@ class CallBase : public Instruction {
18471839
/// Extract the number of dereferenceable bytes for a call or
18481840
/// parameter (0=unknown).
18491841
uint64_t getParamDereferenceableBytes(unsigned i) const {
1850-
return Attrs.getParamDereferenceableBytes(i);
1842+
uint64_t Bytes = Attrs.getParamDereferenceableBytes(i);
1843+
if (const Function *F = getCalledFunction())
1844+
Bytes =
1845+
std::max(Bytes, F->getAttributes().getParamDereferenceableBytes(i));
1846+
return Bytes;
18511847
}
18521848

18531849
/// Extract the number of dereferenceable_or_null bytes for a call
@@ -1865,7 +1861,11 @@ class CallBase : public Instruction {
18651861
/// Extract the number of dereferenceable_or_null bytes for a
18661862
/// parameter (0=unknown).
18671863
uint64_t getParamDereferenceableOrNullBytes(unsigned i) const {
1868-
return Attrs.getParamDereferenceableOrNullBytes(i);
1864+
uint64_t Bytes = Attrs.getParamDereferenceableOrNullBytes(i);
1865+
if (const Function *F = getCalledFunction())
1866+
Bytes = std::max(
1867+
Bytes, F->getAttributes().getParamDereferenceableOrNullBytes(i));
1868+
return Bytes;
18691869
}
18701870

18711871
/// Extract a test mask for disallowed floating-point value classes for the
@@ -1886,9 +1886,7 @@ class CallBase : public Instruction {
18861886
bool isReturnNonNull() const;
18871887

18881888
/// Determine if the return value is marked with NoAlias attribute.
1889-
bool returnDoesNotAlias() const {
1890-
return Attrs.hasRetAttr(Attribute::NoAlias);
1891-
}
1889+
bool returnDoesNotAlias() const { return hasRetAttr(Attribute::NoAlias); }
18921890

18931891
/// If one of the arguments has the 'returned' attribute, returns its
18941892
/// operand value. Otherwise, return nullptr.

llvm/lib/IR/Instructions.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,39 @@ FPClassTest CallBase::getParamNoFPClass(unsigned i) const {
374374
}
375375

376376
std::optional<ConstantRange> CallBase::getRange() const {
377-
const Attribute RangeAttr = getRetAttr(llvm::Attribute::Range);
378-
if (RangeAttr.isValid())
379-
return RangeAttr.getRange();
380-
return std::nullopt;
377+
Attribute CBRangeAttr = getRetAttr(llvm::Attribute::Range);
378+
Attribute FnRangeAttr{};
379+
if (const Function *F = getCalledFunction())
380+
FnRangeAttr = F->getAttributes().getRetAttr(llvm::Attribute::Range);
381+
if (!CBRangeAttr.isValid() || !FnRangeAttr.isValid()) {
382+
if (CBRangeAttr.isValid())
383+
return CBRangeAttr.getRange();
384+
if (FnRangeAttr.isValid())
385+
return FnRangeAttr.getRange();
386+
return std::nullopt;
387+
}
388+
return CBRangeAttr.getRange().intersectWith(FnRangeAttr.getRange());
389+
}
390+
391+
MaybeAlign CallBase::getRetAlign() const {
392+
MaybeAlign CBAlign = Attrs.getRetAlignment();
393+
MaybeAlign FNAlign = CBAlign;
394+
if (const Function *F = getCalledFunction())
395+
FNAlign = F->getAttributes().getRetAlignment();
396+
if (!CBAlign || !FNAlign)
397+
return !CBAlign ? FNAlign : CBAlign;
398+
399+
return MaybeAlign(std::max(CBAlign.valueOrOne(), FNAlign.valueOrOne()));
400+
}
401+
MaybeAlign CallBase::getParamAlign(unsigned ArgNo) const {
402+
MaybeAlign CBAlign = Attrs.getParamAlignment(ArgNo);
403+
MaybeAlign FNAlign = CBAlign;
404+
if (const Function *F = getCalledFunction())
405+
FNAlign = F->getAttributes().getParamAlignment(ArgNo);
406+
if (!CBAlign || !FNAlign)
407+
return !CBAlign ? FNAlign : CBAlign;
408+
409+
return MaybeAlign(std::max(CBAlign.valueOrOne(), FNAlign.valueOrOne()));
381410
}
382411

383412
bool CallBase::isReturnNonNull() const {

llvm/test/CodeGen/AMDGPU/reqd-work-group-size.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ define amdgpu_kernel void @partial_load_group_size_x(ptr addrspace(1) %out) #0 !
353353
; CHECK-LABEL: @partial_load_group_size_x_explicit_callsite_align(
354354
; CHECK-NEXT: %dispatch.ptr = tail call align 2 ptr addrspace(4) @llvm.amdgcn.dispatch.ptr()
355355
; CHECK-NEXT: %gep.group.size.x = getelementptr inbounds i8, ptr addrspace(4) %dispatch.ptr, i64 4
356-
; CHECK-NEXT: %group.size.x.lo = load i8, ptr addrspace(4) %gep.group.size.x, align 2
356+
; CHECK-NEXT: %group.size.x.lo = load i8, ptr addrspace(4) %gep.group.size.x, align 4
357357
; CHECK-NEXT: store i8 %group.size.x.lo, ptr addrspace(1) %out, align 1
358358
define amdgpu_kernel void @partial_load_group_size_x_explicit_callsite_align(ptr addrspace(1) %out) #0 !reqd_work_group_size !0 {
359359
%dispatch.ptr = tail call align 2 ptr addrspace(4) @llvm.amdgcn.dispatch.ptr()

llvm/test/CodeGen/ARM/ssp-data-layout.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,4 @@ declare void @end_struct_large_nonchar()
443443
declare signext i16 @get_struct_small_nonchar()
444444
declare void @end_struct_small_nonchar()
445445

446-
declare void @takes_all(i64, i16, ptr byval(%struct.struct_large_nonchar) align 8, i32, ptr, ptr, ptr, ptr, ptr, i32, i32, i32)
446+
declare void @takes_all(i64, i16, ptr byval(%struct.struct_large_nonchar) align 4, i32, ptr, ptr, ptr, ptr, ptr, i32, i32, i32)

llvm/test/CodeGen/PowerPC/aix-vec-arg-spills-mir.ll

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ define double @caller() {
1717
; MIR32: bb.0.entry:
1818
; MIR32-NEXT: renamable $r3 = LI 0
1919
; MIR32-NEXT: renamable $r4 = LIS 16392
20-
; MIR32-NEXT: STW killed renamable $r4, 180, $r1 :: (store (s32) into unknown-address + 24)
20+
; MIR32-NEXT: STW killed renamable $r4, 184, $r1 :: (store (s32) into unknown-address + 24, align 8)
2121
; MIR32-NEXT: renamable $r4 = LIS 16384
22-
; MIR32-NEXT: STW renamable $r3, 184, $r1 :: (store (s32) into unknown-address + 28)
23-
; MIR32-NEXT: STW renamable $r3, 176, $r1 :: (store (s32) into unknown-address + 20)
24-
; MIR32-NEXT: STW killed renamable $r4, 172, $r1 :: (store (s32) into unknown-address + 16)
25-
; MIR32-NEXT: STW renamable $r3, 168, $r1 :: (store (s32) into unknown-address + 12)
22+
; MIR32-NEXT: STW renamable $r3, 188, $r1 :: (store (s32) into unknown-address + 28, basealign 8)
23+
; MIR32-NEXT: STW renamable $r3, 180, $r1 :: (store (s32) into unknown-address + 20, basealign 8)
24+
; MIR32-NEXT: STW killed renamable $r4, 176, $r1 :: (store (s32) into unknown-address + 16, align 8)
25+
; MIR32-NEXT: STW renamable $r3, 172, $r1 :: (store (s32) into unknown-address + 12, basealign 8)
2626
; MIR32-NEXT: renamable $r4 = LIS 16368
27-
; MIR32-NEXT: STW killed renamable $r4, 164, $r1 :: (store (s32) into unknown-address + 8)
28-
; MIR32-NEXT: STW renamable $r3, 160, $r1 :: (store (s32) into unknown-address + 4)
29-
; MIR32-NEXT: STW killed renamable $r3, 156, $r1 :: (store (s32))
30-
; MIR32-NEXT: ADJCALLSTACKDOWN 188, 0, implicit-def dead $r1, implicit $r1
27+
; MIR32-NEXT: STW killed renamable $r4, 168, $r1 :: (store (s32) into unknown-address + 8, align 8)
28+
; MIR32-NEXT: STW renamable $r3, 164, $r1 :: (store (s32) into unknown-address + 4, basealign 8)
29+
; MIR32-NEXT: STW killed renamable $r3, 160, $r1 :: (store (s32), align 8)
30+
; MIR32-NEXT: ADJCALLSTACKDOWN 192, 0, implicit-def dead $r1, implicit $r1
3131
; MIR32-NEXT: renamable $vsl0 = XXLXORz
3232
; MIR32-NEXT: renamable $r3 = LI 136
3333
; MIR32-NEXT: renamable $r4 = LI 120
@@ -73,22 +73,22 @@ define double @caller() {
7373
; MIR32-NEXT: $f12 = XXLXORdpz
7474
; MIR32-NEXT: $f13 = XXLXORdpz
7575
; MIR32-NEXT: BL_NOP <mcsymbol .callee[PR]>, csr_aix32_altivec, implicit-def dead $lr, implicit $rm, implicit $r3, implicit $r4, implicit $f1, implicit killed $f2, implicit killed $v2, implicit killed $v3, implicit killed $v4, implicit killed $v5, implicit killed $v6, implicit killed $v7, implicit killed $v8, implicit killed $v9, implicit killed $v10, implicit killed $v11, implicit killed $v12, implicit killed $v13, implicit killed $f3, implicit killed $f4, implicit killed $f5, implicit killed $f6, implicit killed $f7, implicit killed $f8, implicit killed $f9, implicit killed $f10, implicit killed $f11, implicit killed $f12, implicit killed $f13, implicit $r2, implicit-def $r1, implicit-def $f1
76-
; MIR32-NEXT: ADJCALLSTACKUP 188, 0, implicit-def dead $r1, implicit $r1
76+
; MIR32-NEXT: ADJCALLSTACKUP 192, 0, implicit-def dead $r1, implicit $r1
7777
; MIR32-NEXT: BLR implicit $lr, implicit $rm, implicit $f1
7878
;
7979
; MIR64-LABEL: name: caller
8080
; MIR64: bb.0.entry:
8181
; MIR64-NEXT: renamable $x3 = LI8 2049
8282
; MIR64-NEXT: renamable $x4 = LI8 1
8383
; MIR64-NEXT: renamable $x3 = RLDIC killed renamable $x3, 51, 1
84-
; MIR64-NEXT: STD killed renamable $x3, 216, $x1 :: (store (s64) into unknown-address + 24, align 4)
84+
; MIR64-NEXT: STD killed renamable $x3, 216, $x1 :: (store (s64) into unknown-address + 24)
8585
; MIR64-NEXT: renamable $x3 = LI8 1023
8686
; MIR64-NEXT: renamable $x4 = RLDIC killed renamable $x4, 62, 1
87-
; MIR64-NEXT: STD killed renamable $x4, 208, $x1 :: (store (s64) into unknown-address + 16, align 4)
87+
; MIR64-NEXT: STD killed renamable $x4, 208, $x1 :: (store (s64) into unknown-address + 16)
8888
; MIR64-NEXT: renamable $x4 = LI8 0
89-
; MIR64-NEXT: STD renamable $x4, 192, $x1 :: (store (s64), align 4)
89+
; MIR64-NEXT: STD renamable $x4, 192, $x1 :: (store (s64))
9090
; MIR64-NEXT: renamable $x3 = RLDIC killed renamable $x3, 52, 2
91-
; MIR64-NEXT: STD killed renamable $x3, 200, $x1 :: (store (s64) into unknown-address + 8, align 4)
91+
; MIR64-NEXT: STD killed renamable $x3, 200, $x1 :: (store (s64) into unknown-address + 8)
9292
; MIR64-NEXT: ADJCALLSTACKDOWN 224, 0, implicit-def dead $r1, implicit $r1
9393
; MIR64-NEXT: renamable $vsl0 = XXLXORz
9494
; MIR64-NEXT: renamable $x3 = LI8 160

llvm/test/CodeGen/PowerPC/aix-vec-arg-spills.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ define double @caller() {
2020
; 32BIT-NEXT: li 3, 0
2121
; 32BIT-NEXT: xxlxor 0, 0, 0
2222
; 32BIT-NEXT: xxlxor 1, 1, 1
23-
; 32BIT-NEXT: stw 4, 180(1)
23+
; 32BIT-NEXT: stw 4, 184(1)
2424
; 32BIT-NEXT: lis 4, 16384
25-
; 32BIT-NEXT: stw 3, 184(1)
26-
; 32BIT-NEXT: stw 3, 176(1)
27-
; 32BIT-NEXT: stw 4, 172(1)
25+
; 32BIT-NEXT: stw 3, 188(1)
26+
; 32BIT-NEXT: stw 3, 180(1)
27+
; 32BIT-NEXT: stw 4, 176(1)
2828
; 32BIT-NEXT: lis 4, 16368
29-
; 32BIT-NEXT: stw 3, 168(1)
29+
; 32BIT-NEXT: stw 3, 172(1)
30+
; 32BIT-NEXT: stw 3, 164(1)
31+
; 32BIT-NEXT: stw 4, 168(1)
3032
; 32BIT-NEXT: stw 3, 160(1)
31-
; 32BIT-NEXT: stw 4, 164(1)
32-
; 32BIT-NEXT: stw 3, 156(1)
3333
; 32BIT-NEXT: li 3, 136
3434
; 32BIT-NEXT: li 4, 120
3535
; 32BIT-NEXT: xxlxor 2, 2, 2

0 commit comments

Comments
 (0)