Skip to content

[X86] Improve illegal return type handling in FastISel#186723

Merged
LuoYuanke merged 2 commits intollvm:mainfrom
LuoYuanke:fast-isel-compiling-time
Mar 17, 2026
Merged

[X86] Improve illegal return type handling in FastISel#186723
LuoYuanke merged 2 commits intollvm:mainfrom
LuoYuanke:fast-isel-compiling-time

Conversation

@LuoYuanke
Copy link
Copy Markdown
Contributor

Previously, FastISel would fall back to DAG ISel for any illegal return
type. This change adds a more precise check to determine if the ABI
requires a type conversion that FastISel cannot handle.

For example, bfloat is returned as f16 in XMM0, but FastISel would
assign f32 register type and store it in FuncInfo.ValueMap, causing DAG
to incorrectly perform type conversion from f32 to bfloat later.

However, i1 is promoted to i8 and returned as i8 per the ABI, so FastISel
can safely lower it without switching to DAGISel. This change enables
FastISel to handle such cases properly.

Previously, FastISel would fall back to DAG ISel for any illegal return
type. This change adds a more precise check to determine if the ABI
requires a type conversion that FastISel cannot handle.

For example, bfloat is returned as f16 in XMM0, but FastISel would
assign f32 register type and store it in FuncInfo.ValueMap, causing DAG
to incorrectly perform type conversion from f32 to bfloat later.

However, i1 is promoted to i8 and returned as i8 per the ABI, so FastISel
can safely lower it without switching to DAGISel. This change enables
FastISel to handle such cases properly.
@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Mar 16, 2026

@llvm/pr-subscribers-backend-x86

Author: Luo Yuanke (LuoYuanke)

Changes

Previously, FastISel would fall back to DAG ISel for any illegal return
type. This change adds a more precise check to determine if the ABI
requires a type conversion that FastISel cannot handle.

For example, bfloat is returned as f16 in XMM0, but FastISel would
assign f32 register type and store it in FuncInfo.ValueMap, causing DAG
to incorrectly perform type conversion from f32 to bfloat later.

However, i1 is promoted to i8 and returned as i8 per the ABI, so FastISel
can safely lower it without switching to DAGISel. This change enables
FastISel to handle such cases properly.


Full diff: https://github.com/llvm/llvm-project/pull/186723.diff

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86FastISel.cpp (+18-4)
  • (added) llvm/test/CodeGen/X86/i1-fast-isel.ll (+106)
diff --git a/llvm/lib/Target/X86/X86FastISel.cpp b/llvm/lib/Target/X86/X86FastISel.cpp
index 0093a6b29d226..67e5a2ec869c4 100644
--- a/llvm/lib/Target/X86/X86FastISel.cpp
+++ b/llvm/lib/Target/X86/X86FastISel.cpp
@@ -3200,11 +3200,25 @@ bool X86FastISel::fastLowerCall(CallLoweringInfo &CLI) {
   bool Is64Bit        = Subtarget->is64Bit();
   bool IsWin64        = Subtarget->isCallingConvWin64(CC);
 
-  // If the return type is illegal, don't bother to promote it, just fall back
-  // to DAG ISel.
+  // If the return type is illegal, check if the ABI requires a type conversion
+  // that FastISel cannot handle. Fall back to DAG ISel in such cases.
+  // For example, bfloat is returned as f16 in XMM0, however FastISel would
+  // assign f32 register type and store it in FuncInfo.ValueMap. This would
+  // cause DAG incorrectly perform type conversion from f32 to bfloat after get
+  // the value from FuncInfo.ValueMap.
+  // However, i1 is promoted to i8 and return i8 defined by ABI, so FastISel can
+  // lower it without switching to DAGISel.
   MVT RetVT;
-  if (!isTypeLegal(CLI.RetTy, RetVT) && !CLI.RetTy->isVoidTy())
-    return false;
+  if (!isTypeLegal(CLI.RetTy, RetVT) && !CLI.RetTy->isVoidTy()) {
+    EVT RetEVT = EVT::getEVT(CLI.RetTy, /*HandleUnknown=*/true);
+    if (RetEVT == MVT::Other)
+      return false; // Unknown type, let DAG ISel handle it.
+    EVT RetABIEVT = TLI.getRegisterTypeForCallingConv(CLI.RetTy->getContext(),
+                                                      CLI.CallConv, RetEVT);
+    MVT RegVT = TLI.getRegisterType(CLI.RetTy->getContext(), RetEVT);
+    if (RetABIEVT != RegVT)
+      return false;
+  }
 
   // Call / invoke instructions with NoCfCheck attribute require special
   // handling.
diff --git a/llvm/test/CodeGen/X86/i1-fast-isel.ll b/llvm/test/CodeGen/X86/i1-fast-isel.ll
new file mode 100644
index 0000000000000..1f111129de13c
--- /dev/null
+++ b/llvm/test/CodeGen/X86/i1-fast-isel.ll
@@ -0,0 +1,106 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc --fast-isel < %s -mtriple=x86_64-unknown-unknown | FileCheck %s
+
+define i8 @test_direct_call(ptr %f) nounwind {
+; CHECK-LABEL: test_direct_call:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    pushq %rax
+; CHECK-NEXT:    callq foo@PLT
+; CHECK-NEXT:    movzbl %al, %edi
+; CHECK-NEXT:    callq bar@PLT
+; CHECK-NEXT:    popq %rcx
+; CHECK-NEXT:    retq
+entry:
+  %call = call i1 @foo(ptr %f)
+  %call2 = call zeroext i8 @bar(i1 %call)
+  ret i8 %call2
+}
+
+define i8 @test_fast_direct_call(ptr %f) nounwind {
+; CHECK-LABEL: test_fast_direct_call:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    pushq %rax
+; CHECK-NEXT:    callq foo_fast@PLT
+; CHECK-NEXT:    movzbl %al, %edi
+; CHECK-NEXT:    callq bar@PLT
+; CHECK-NEXT:    popq %rcx
+; CHECK-NEXT:    retq
+entry:
+  %call = call fastcc i1 @foo_fast(ptr %f)
+  %call2 = call zeroext i8 @bar(i1 %call)
+  ret i8 %call2
+}
+
+define i8 @test_indirect_all(ptr %fptr, ptr %f) nounwind {
+; CHECK-LABEL: test_indirect_all:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    pushq %rbx
+; CHECK-NEXT:    movq %rdi, %rbx
+; CHECK-NEXT:    movq %rsi, %rdi
+; CHECK-NEXT:    callq foo@PLT
+; CHECK-NEXT:    movzbl %al, %edi
+; CHECK-NEXT:    callq *%rbx
+; CHECK-NEXT:    popq %rbx
+; CHECK-NEXT:    retq
+entry:
+  %call = call i1 @foo(ptr %f)
+  %call2 = call zeroext i8 %fptr(i1 %call)
+  ret i8 %call2
+}
+
+define i8 @test_indirect_all2(ptr %fptr, ptr %f, i1 %cond) nounwind {
+; CHECK-LABEL: test_indirect_all2:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    pushq %rbp
+; CHECK-NEXT:    pushq %rbx
+; CHECK-NEXT:    pushq %rax
+; CHECK-NEXT:    movl %edx, %ebp
+; CHECK-NEXT:    movq %rdi, %rbx
+; CHECK-NEXT:    movq %rsi, %rdi
+; CHECK-NEXT:    callq foo@PLT
+; CHECK-NEXT:    testb $1, %bpl
+; CHECK-NEXT:    je .LBB3_2
+; CHECK-NEXT:  # %bb.1: # %exit
+; CHECK-NEXT:    movzbl %al, %edi
+; CHECK-NEXT:    callq *%rbx
+; CHECK-NEXT:    jmp .LBB3_3
+; CHECK-NEXT:  .LBB3_2: # %exit2
+; CHECK-NEXT:    movb $3, %al
+; CHECK-NEXT:  .LBB3_3: # %exit2
+; CHECK-NEXT:    addq $8, %rsp
+; CHECK-NEXT:    popq %rbx
+; CHECK-NEXT:    popq %rbp
+; CHECK-NEXT:    retq
+entry:
+  %call = call i1 @foo(ptr %f)
+  br i1 %cond, label %exit, label %exit2
+
+exit:
+  %call2 = call zeroext i8 %fptr(i1 %call)
+  ret i8 %call2
+
+exit2:
+  ret i8 3
+}
+
+
+define i8 @test_fast_indirect_all(ptr %fptr, ptr %f) nounwind {
+; CHECK-LABEL: test_fast_indirect_all:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    pushq %rbx
+; CHECK-NEXT:    movq %rdi, %rbx
+; CHECK-NEXT:    movq %rsi, %rdi
+; CHECK-NEXT:    callq foo@PLT
+; CHECK-NEXT:    movzbl %al, %edi
+; CHECK-NEXT:    callq *%rbx
+; CHECK-NEXT:    popq %rbx
+; CHECK-NEXT:    retq
+entry:
+  %call = call fastcc i1 @foo(ptr %f)
+  %call2 = call zeroext i8 %fptr(i1 %call)
+  ret i8 %call2
+}
+
+declare i1 @foo(ptr %f)
+declare zeroext i8 @bar(i1)
+declare fastcc i1 @foo_fast(ptr %f)

return false; // Unknown type, let DAG ISel handle it.
EVT RetABIEVT = TLI.getRegisterTypeForCallingConv(CLI.RetTy->getContext(),
CLI.CallConv, RetEVT);
MVT RegVT = TLI.getRegisterType(CLI.RetTy->getContext(), RetEVT);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRegisterType require MVT. Should check isSimple() before it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revised. Now I use RetVT from isTypeLegal(). It must be simple type here, otherwise isTypeLegal would assign a type other than MVT::Other to RetVT.

if (!isTypeLegal(CLI.RetTy, RetVT) && !CLI.RetTy->isVoidTy())
return false;
if (!isTypeLegal(CLI.RetTy, RetVT) && !CLI.RetTy->isVoidTy()) {
EVT RetEVT = EVT::getEVT(CLI.RetTy, /*HandleUnknown=*/true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use RetVT here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revised.

EVT RetEVT = EVT::getEVT(CLI.RetTy, /*HandleUnknown=*/true);
if (RetEVT == MVT::Other)
return false; // Unknown type, let DAG ISel handle it.
EVT RetABIEVT = TLI.getRegisterTypeForCallingConv(CLI.RetTy->getContext(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRegisterTypeForCallingConv return MVT too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revised

@nikic
Copy link
Copy Markdown
Contributor

nikic commented Mar 16, 2026

@LuoYuanke LuoYuanke requested a review from phoebewang March 16, 2026 11:56
Copy link
Copy Markdown
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@LuoYuanke LuoYuanke merged commit 140adc9 into llvm:main Mar 17, 2026
10 checks passed
@LuoYuanke LuoYuanke deleted the fast-isel-compiling-time branch March 17, 2026 00:20
neonetizen pushed a commit to neonetizen/llvm-project that referenced this pull request Mar 17, 2026
Previously, FastISel would fall back to DAG ISel for any illegal return
type. This change adds a more precise check to determine if the ABI
requires a type conversion that FastISel cannot handle.

For example, bfloat is returned as f16 in XMM0, but FastISel would
assign f32 register type and store it in FuncInfo.ValueMap, causing DAG
to incorrectly perform type conversion from f32 to bfloat later.

However, i1 is promoted to i8 and returned as i8 per the ABI, so
FastISel
can safely lower it without switching to DAGISel. This change enables
FastISel to handle such cases properly.

---------

Co-authored-by: Yuanke Luo <ykluo@birentech.com>
dianqk pushed a commit to dianqk/llvm-project that referenced this pull request Mar 27, 2026
Previously, FastISel would fall back to DAG ISel for any illegal return
type. This change adds a more precise check to determine if the ABI
requires a type conversion that FastISel cannot handle.

For example, bfloat is returned as f16 in XMM0, but FastISel would
assign f32 register type and store it in FuncInfo.ValueMap, causing DAG
to incorrectly perform type conversion from f32 to bfloat later.

However, i1 is promoted to i8 and returned as i8 per the ABI, so
FastISel
can safely lower it without switching to DAGISel. This change enables
FastISel to handle such cases properly.

---------

Co-authored-by: Yuanke Luo <ykluo@birentech.com>
(cherry picked from commit 140adc9)
@dianqk dianqk added this to the LLVM 22.x Release milestone Mar 28, 2026
@dianqk
Copy link
Copy Markdown
Member

dianqk commented Mar 28, 2026

/cherry-pick 140adc9

@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Mar 28, 2026

/pull-request #189139

rust-bors bot pushed a commit to rust-lang/rust that referenced this pull request Mar 29, 2026
[perf] Revert FastISel patch

This caused a significant compile-time regression for debug builds.

There is another change (llvm/llvm-project#186723) that mitigates that regression, but not fully. Revert it for now.
rust-bors bot pushed a commit to rust-lang/rust that referenced this pull request Mar 30, 2026
[perf] Revert FastISel patch

This caused a significant compile-time regression for debug builds.

There is another change (llvm/llvm-project#186723) that mitigates that regression, but not fully. Revert it for now.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Mar 30, 2026
[perf] Revert FastISel patch

This caused a significant compile-time regression for debug builds.

There is another change (llvm/llvm-project#186723) that mitigates that regression, but not fully. Revert it for now.
c-rhodes pushed a commit to llvmbot/llvm-project that referenced this pull request Mar 30, 2026
Previously, FastISel would fall back to DAG ISel for any illegal return
type. This change adds a more precise check to determine if the ABI
requires a type conversion that FastISel cannot handle.

For example, bfloat is returned as f16 in XMM0, but FastISel would
assign f32 register type and store it in FuncInfo.ValueMap, causing DAG
to incorrectly perform type conversion from f32 to bfloat later.

However, i1 is promoted to i8 and returned as i8 per the ABI, so
FastISel
can safely lower it without switching to DAGISel. This change enables
FastISel to handle such cases properly.

---------

Co-authored-by: Yuanke Luo <ykluo@birentech.com>
(cherry picked from commit 140adc9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

5 participants