Repository navigation
[X86][Inline] Make ABI compatibility check more precise - #205106
Conversation
|
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-backend-x86 Author: Nikita Popov (nikic) ChangesWhen inlining a function that contains calls with vector arguments, we have to be careful that inlining does not change the ABI of the call. E.g. we generally can't inline a function without However, the current check is very crude and only allows inlining with interior calls if the target features match exactly (via the base areTypesABICompatible implementation). This is unnecessarily conservative, as many target features do not affect the call ABI at all. Make this check more precise by checking the result of getRegisterTypeForCallingConv for the type between the TLI instances for the caller and callee. 3 Files Affected:
diff --git a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
index 0e9d8679ca9ec..7b00502ce1c6c 100644
--- a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
+++ b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
@@ -6765,23 +6765,19 @@ bool X86TTIImpl::areInlineCompatible(const Function *Caller,
bool X86TTIImpl::areTypesABICompatible(const Function *Caller,
const Function *Callee,
ArrayRef<Type *> Types) const {
- if (!BaseT::areTypesABICompatible(Caller, Callee, Types))
- return false;
-
- // If we get here, we know the target features match. If one function
- // considers 512-bit vectors legal and the other does not, consider them
- // incompatible.
const TargetMachine &TM = getTLI()->getTargetMachine();
-
- if (TM.getSubtarget<X86Subtarget>(*Caller).useAVX512Regs() ==
- TM.getSubtarget<X86Subtarget>(*Callee).useAVX512Regs())
- return true;
-
- // Consider the arguments compatible if they aren't vectors or aggregates.
- // FIXME: Look at the size of vectors.
- // FIXME: Look at the element types of aggregates to see if there are vectors.
- return llvm::none_of(Types,
- [](Type *T) { return T->isVectorTy() || T->isAggregateType(); });
+ const TargetLowering *CallerTLI =
+ TM.getSubtargetImpl(*Caller)->getTargetLowering();
+ const TargetLowering *CalleeTLI =
+ TM.getSubtargetImpl(*Callee)->getTargetLowering();
+
+ LLVMContext &Ctx = Caller->getContext();
+ CallingConv::ID CC = Callee->getCallingConv();
+ return all_of(Types, [&](Type *Ty) {
+ EVT VT = CallerTLI->getValueType(DL, Ty);
+ return CallerTLI->getRegisterTypeForCallingConv(Ctx, CC, VT) ==
+ CalleeTLI->getRegisterTypeForCallingConv(Ctx, CC, VT);
+ });
}
X86TTIImpl::TTI::MemCmpExpansionOptions
diff --git a/llvm/test/Transforms/Inline/X86/call-abi-compatibility.ll b/llvm/test/Transforms/Inline/X86/call-abi-compatibility.ll
index 9d0981cd63756..158fa4471d542 100644
--- a/llvm/test/Transforms/Inline/X86/call-abi-compatibility.ll
+++ b/llvm/test/Transforms/Inline/X86/call-abi-compatibility.ll
@@ -143,5 +143,138 @@ define internal <8 x i64> @callee_inline_asm(ptr %p0, i64 %k, ptr %p1, ptr %p2)
ret <8 x i64> %3
}
+declare void @callee_128bit(<4 x float>)
+declare void @callee_256bit(<8 x float>)
+declare void @callee_512bit(<16 x float>)
+
+define void @callee_calls_128bit_baseline() {
+; CHECK-LABEL: define {{[^@]+}}@callee_calls_128bit_baseline() {
+; CHECK-NEXT: call void @callee_128bit(<4 x float> zeroinitializer)
+; CHECK-NEXT: ret void
+;
+ call void @callee_128bit(<4 x float> zeroinitializer)
+ ret void
+}
+
+; Okay to inline as +sse3 does not change the ABI of 128 bit vectors relative
+; to x86-64 baseline.
+define void @caller_calls_128bit_sse3() "target-features"="+sse3" {
+; CHECK-LABEL: define {{[^@]+}}@caller_calls_128bit_sse3
+; CHECK-SAME: () #[[ATTR3:[0-9]+]] {
+; CHECK-NEXT: call void @callee_128bit(<4 x float> zeroinitializer)
+; CHECK-NEXT: ret void
+;
+ call void @callee_calls_128bit_baseline()
+ ret void
+}
+
+define void @callee_calls_256bit_baseline() {
+; CHECK-LABEL: define {{[^@]+}}@callee_calls_256bit_baseline() {
+; CHECK-NEXT: call void @callee_256bit(<8 x float> zeroinitializer)
+; CHECK-NEXT: ret void
+;
+ call void @callee_256bit(<8 x float> zeroinitializer)
+ ret void
+}
+
+; Okay to inline as +sse3 does not change the ABI of 256 bit vectors relative
+; to x86-64 baseline.
+define void @caller_calls_256bit_sse3() "target-features"="+sse3" {
+; CHECK-LABEL: define {{[^@]+}}@caller_calls_256bit_sse3
+; CHECK-SAME: () #[[ATTR3]] {
+; CHECK-NEXT: call void @callee_256bit(<8 x float> zeroinitializer)
+; CHECK-NEXT: ret void
+;
+ call void @callee_calls_256bit_baseline()
+ ret void
+}
+
+; Can NOT inline as +avx changes the ABI of 256 bit vectors.
+define void @caller_calls_256bit_avx() "target-features"="+avx" {
+; CHECK-LABEL: define {{[^@]+}}@caller_calls_256bit_avx
+; CHECK-SAME: () #[[ATTR0]] {
+; CHECK-NEXT: call void @callee_calls_256bit_baseline()
+; CHECK-NEXT: ret void
+;
+ call void @callee_calls_256bit_baseline()
+ ret void
+}
+
+define void @callee_calls_256bit_avx() "target-features"="+avx" {
+; CHECK-LABEL: define {{[^@]+}}@callee_calls_256bit_avx
+; CHECK-SAME: () #[[ATTR0]] {
+; CHECK-NEXT: call void @callee_256bit(<8 x float> zeroinitializer)
+; CHECK-NEXT: ret void
+;
+ call void @callee_256bit(<8 x float> zeroinitializer)
+ ret void
+}
+
+; Okay to inline as +avx2 does not change the ABI of 256 bit vectors relative
+; to +avx.
+define void @caller_calls_256bit_avx2() "target-features"="+avx2" {
+; CHECK-LABEL: define {{[^@]+}}@caller_calls_256bit_avx2
+; CHECK-SAME: () #[[ATTR4:[0-9]+]] {
+; CHECK-NEXT: call void @callee_256bit(<8 x float> zeroinitializer)
+; CHECK-NEXT: ret void
+;
+ call void @callee_calls_256bit_avx()
+ ret void
+}
+
+define void @callee_calls_512bit_baseline() {
+; CHECK-LABEL: define {{[^@]+}}@callee_calls_512bit_baseline() {
+; CHECK-NEXT: call void @callee_512bit(<16 x float> zeroinitializer)
+; CHECK-NEXT: ret void
+;
+ call void @callee_512bit(<16 x float> zeroinitializer)
+ ret void
+}
+
+; Okay to inline as +sse3 does not change the ABI of 512 bit vectors relative
+; to x86-64 baseline.
+define void @caller_calls_512bit_sse3() "target-features"="+sse3" {
+; CHECK-LABEL: define {{[^@]+}}@caller_calls_512bit_sse3
+; CHECK-SAME: () #[[ATTR3]] {
+; CHECK-NEXT: call void @callee_512bit(<16 x float> zeroinitializer)
+; CHECK-NEXT: ret void
+;
+ call void @callee_calls_512bit_baseline()
+ ret void
+}
+
+; Can NOT inline as +avx changes the ABI of 512 bit vectors.
+define void @caller_calls_512bit_avx() "target-features"="+avx" {
+; CHECK-LABEL: define {{[^@]+}}@caller_calls_512bit_avx
+; CHECK-SAME: () #[[ATTR0]] {
+; CHECK-NEXT: call void @callee_calls_512bit_baseline()
+; CHECK-NEXT: ret void
+;
+ call void @callee_calls_512bit_baseline()
+ ret void
+}
+
+define void @callee_calls_512bit_avx512f() "target-features"="+avx512f" {
+; CHECK-LABEL: define {{[^@]+}}@callee_calls_512bit_avx512f
+; CHECK-SAME: () #[[ATTR5:[0-9]+]] {
+; CHECK-NEXT: call void @callee_512bit(<16 x float> zeroinitializer)
+; CHECK-NEXT: ret void
+;
+ call void @callee_512bit(<16 x float> zeroinitializer)
+ ret void
+}
+
+; Okay to inline as +avx512bw does not change the ABI of 512 bit vectors
+;relative to +avx512f.
+define void @caller_calls_512bit_avx512bw() "target-features"="+avx512bw" {
+; CHECK-LABEL: define {{[^@]+}}@caller_calls_512bit_avx512bw
+; CHECK-SAME: () #[[ATTR6:[0-9]+]] {
+; CHECK-NEXT: call void @callee_512bit(<16 x float> zeroinitializer)
+; CHECK-NEXT: ret void
+;
+ call void @callee_calls_512bit_avx512f()
+ ret void
+}
+
attributes #0 = { "min-legal-vector-width"="512" "target-features"="+avx,+avx2,+avx512bw,+avx512dq,+avx512f,+cmov,+crc32,+cx8,+evex512,+f16c,+fma,+fxsr,+mmx,+popcnt,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87,+xsave" "tune-cpu"="generic" }
attributes #1 = { "min-legal-vector-width"="512" "target-features"="+avx,+avx2,+avx512bw,+avx512f,+cmov,+crc32,+cx8,+evex512,+f16c,+fma,+fxsr,+mmx,+popcnt,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87,+xsave" "tune-cpu"="generic" }
diff --git a/llvm/test/Transforms/PhaseOrdering/X86/loop-vectorizer-noalias.ll b/llvm/test/Transforms/PhaseOrdering/X86/loop-vectorizer-noalias.ll
index 112cc75b1cf05..96a7e2d4c52b3 100644
--- a/llvm/test/Transforms/PhaseOrdering/X86/loop-vectorizer-noalias.ll
+++ b/llvm/test/Transforms/PhaseOrdering/X86/loop-vectorizer-noalias.ll
@@ -27,9 +27,9 @@ define void @accsum(ptr noundef %vals, i64 noundef %num) #0 {
; CHECK-NEXT: [[STORE_FORWARDED:%.*]] = phi i8 [ [[LOAD_INITIAL]], [[FOR_BODY_PREHEADER]] ], [ [[ADD_I:%.*]], [[FOR_BODY]] ]
; CHECK-NEXT: [[I_02:%.*]] = phi i64 [ 1, [[FOR_BODY_PREHEADER]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i8, ptr [[VALS]], i64 [[I_02]]
-; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[ARRAYIDX]], align 1, !alias.scope [[META0:![0-9]+]], !noalias [[META3:![0-9]+]]
+; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[ARRAYIDX]], align 1, !alias.scope [[META0:![0-9]+]]
; CHECK-NEXT: [[ADD_I]] = add i8 [[TMP0]], [[STORE_FORWARDED]]
-; CHECK-NEXT: store i8 [[ADD_I]], ptr [[ARRAYIDX]], align 1, !alias.scope [[META0]], !noalias [[META3]]
+; CHECK-NEXT: store i8 [[ADD_I]], ptr [[ARRAYIDX]], align 1, !alias.scope [[META0]]
; CHECK-NEXT: [[INC]] = add nuw i64 [[I_02]], 1
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[INC]], [[NUM]]
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_END]], label [[FOR_BODY]]
@@ -73,6 +73,4 @@ attributes #0 = { "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87"}
; CHECK: [[META0]] = !{[[META1:![0-9]+]]}
; CHECK: [[META1]] = distinct !{[[META1]], [[META2:![0-9]+]], !"acc: %val"}
; CHECK: [[META2]] = distinct !{[[META2]], !"acc"}
-; CHECK: [[META3]] = !{[[META4:![0-9]+]]}
-; CHECK: [[META4]] = distinct !{[[META4]], [[META2]], !"acc: %prev"}
;.
|
There was a problem hiding this comment.
The change to this test is because one of the i8 loads gets argument promoted (which is legal, as target features do not change the i8 call ABI).
There was a problem hiding this comment.
Maybe we can skip scalar type as it should be ABI compatible for any features. Outliers like fp80, fp16 etc. should be blocked in the front end already.
There was a problem hiding this comment.
Just to be clear, the change here is because we now report i8 as ABI compatible, not the other way around. I don't think there's a need to special case scalar types, they are handled correctly by the general check.
|
|
||
| ; Okay to inline as +avx512bw does not change the ABI of 512 bit vectors | ||
| ;relative to +avx512f. | ||
| define void @caller_calls_512bit_avx512bw() "target-features"="+avx512bw" { |
There was a problem hiding this comment.
Does it work if one has "min-legal-vector-width"="256"?
There was a problem hiding this comment.
I'm always confused by this attribute. Do you have an example I can test? I wasn't able to come up with a case where this attribute actually changes the call ABI: https://llvm.godbolt.org/z/anPMvEdv8
(Note that there is existing adjustment for the attribute during inlining.)
There was a problem hiding this comment.
I managed to get an ABI difference using avx512vl and setting both min-legal-vector-width and prefer-vector-width: https://llvm.godbolt.org/z/Yezz8r7n8 I have no idea why you need precisely that incantation...
There was a problem hiding this comment.
I think that's what the previous code prevent for:
if (TM.getSubtarget<X86Subtarget>(*Caller).useAVX512Regs() ==
TM.getSubtarget<X86Subtarget>(*Callee).useAVX512Regs())
return true;
I'm surprised no test case to cover it.
There was a problem hiding this comment.
There is a test case for this here: https://github.com/llvm/llvm-project/blob/main/llvm/test/Transforms/ArgumentPromotion/X86/min-legal-vector-width.ll The new implementation hands this correctly.
However, I found that there is a pre-existing issue that the inlining ABI check (unlike the one in ArgPromotion) does not handle this, because it bails out early if the target features match exactly, so it never gets to this code. I've pushed an extra commit to fix this, so we now only skip the ABI check if both the features are the same and useAVX512Regs is the same.
There was a problem hiding this comment.
This looks correct, but does ArgPromotion not need to check useAVX512Regs now?
There was a problem hiding this comment.
It doesn't need the explicit check anymore, because getRegisterTypeForCallingConv() will handle it (depending on useAVX512Regs the 512-bit vectors will either be legal or not, and that affects the register type used for the calling convention).
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/66/builds/32657 Here is the relevant piece of the build log for the reference |
Fixes a regression from #205106. getValueType() asserts on aggregate types. Use CompuateValueVTs() to compute the de-aggregated VTs. Performing argument promotion for struct types seems pretty dubious to me, but it was previously allowed, so I'm retaining that behavior. We may want to disable promotion of aggregates in ArgPromotion entirely though.
When inlining a function that contains calls with vector arguments, we have to be careful that inlining does not change the ABI of the call. E.g. we generally can't inline a function without `+avx` into a function with `+avx` if there are calls using vectors of size 256 or larger, because they'd switch from passing in two xmm registers to passing in a ymm register. However, the current check is very crude and only allows inlining with interior calls if the target features match *exactly* (via the base areTypesABICompatible implementation). This is unnecessarily conservative, as many target features do not affect the call ABI at all. Make this check more precise by checking the result of getRegisterTypeForCallingConv for the type between the TLI instances for the caller and callee.
Fixes a regression from llvm#205106. getValueType() asserts on aggregate types. Use CompuateValueVTs() to compute the de-aggregated VTs. Performing argument promotion for struct types seems pretty dubious to me, but it was previously allowed, so I'm retaining that behavior. We may want to disable promotion of aggregates in ArgPromotion entirely though.
…#205113) If inlining is unsound due to incompatible target feature attributes, we should not inline the call even if alwaysinline is set. This will likely result in a crash during instruction selection. We tried this previously in d6f994a, but the change had to be reverted because the quality of our areInlineCompatible() hooks was very bad at the time, which resulted in inlining not happening in many cases where it was safe. I think we're in a much better position now. Most notably, we now have a default areInlineCompatible() implementation that actually does something sensible (#117493), inlining compatibility for target features is now specified in TableGen (#205348). Various target-specific issues have been fixed as well, e.g. ARM's overly strict feature whitelist (#205763) and X86's overly conservative ABI compatibility checks (#205106).
…et features (#205113) If inlining is unsound due to incompatible target feature attributes, we should not inline the call even if alwaysinline is set. This will likely result in a crash during instruction selection. We tried this previously in llvm/llvm-project@d6f994a, but the change had to be reverted because the quality of our areInlineCompatible() hooks was very bad at the time, which resulted in inlining not happening in many cases where it was safe. I think we're in a much better position now. Most notably, we now have a default areInlineCompatible() implementation that actually does something sensible (llvm/llvm-project#117493), inlining compatibility for target features is now specified in TableGen (llvm/llvm-project#205348). Various target-specific issues have been fixed as well, e.g. ARM's overly strict feature whitelist (llvm/llvm-project#205763) and X86's overly conservative ABI compatibility checks (llvm/llvm-project#205106).
…et features (#205113) If inlining is unsound due to incompatible target feature attributes, we should not inline the call even if alwaysinline is set. This will likely result in a crash during instruction selection. We tried this previously in llvm/llvm-project@d6f994a, but the change had to be reverted because the quality of our areInlineCompatible() hooks was very bad at the time, which resulted in inlining not happening in many cases where it was safe. I think we're in a much better position now. Most notably, we now have a default areInlineCompatible() implementation that actually does something sensible (llvm/llvm-project#117493), inlining compatibility for target features is now specified in TableGen (llvm/llvm-project#205348). Various target-specific issues have been fixed as well, e.g. ARM's overly strict feature whitelist (llvm/llvm-project#205763) and X86's overly conservative ABI compatibility checks (llvm/llvm-project#205106).
…et features (#205113) If inlining is unsound due to incompatible target feature attributes, we should not inline the call even if alwaysinline is set. This will likely result in a crash during instruction selection. We tried this previously in llvm/llvm-project@d6f994a, but the change had to be reverted because the quality of our areInlineCompatible() hooks was very bad at the time, which resulted in inlining not happening in many cases where it was safe. I think we're in a much better position now. Most notably, we now have a default areInlineCompatible() implementation that actually does something sensible (llvm/llvm-project#117493), inlining compatibility for target features is now specified in TableGen (llvm/llvm-project#205348). Various target-specific issues have been fixed as well, e.g. ARM's overly strict feature whitelist (llvm/llvm-project#205763) and X86's overly conservative ABI compatibility checks (llvm/llvm-project#205106).
When inlining a function that contains calls with vector arguments, we have to be careful that inlining does not change the ABI of the call. E.g. we generally can't inline a function without
+avxinto a function with+avxif there are calls using vectors of size 256 or larger, because they'd switch from passing in two xmm registers to passing in a ymm register.However, the current check is very crude and only allows inlining with interior calls if the target features match exactly (via the base areTypesABICompatible implementation). This is unnecessarily conservative, as many target features do not affect the call ABI at all.
Make this check more precise by checking the result of getRegisterTypeForCallingConv for the type between the TLI instances for the caller and callee.