Skip to content

Conversation

@nikic
Copy link
Contributor

@nikic nikic commented Dec 11, 2025

The fast instruction selector should should not force an SDAG fallback to potentially make use of optimized libcall implementations.

Looking at 3e6fa46, part of the motivation was to avoid libcalls in unoptimized builds for targets that don't have them, but I believe this should be handled by Clang directly emitting intrinsics instead of libcalls (which it already does). FastISel should not second guess this.

Followup to #171288.

The fast instruction selector should should not force an SDAG
fallback to potentially make use of optimized libcall
implementations.

Note that clang will already directly emit intrinsics for most
of these anyway, so there should be little change in practice.
@nikic nikic requested a review from arsenm December 11, 2025 09:01
@llvmbot llvmbot added backend:X86 llvm:SelectionDAG SelectionDAGISel as well labels Dec 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 11, 2025

@llvm/pr-subscribers-backend-x86

Author: Nikita Popov (nikic)

Changes

The fast instruction selector should should not force an SDAG fallback to potentially make use of optimized libcall implementations.

Looking at 3e6fa46, part of the motivation was to avoid libcalls in unoptimized builds for targets that don't have them, but I believe this should be handled by Clang directly emitting intrinsics instead of libcalls (which it already does). FastISel should not second guess this.

Followup to #171288.


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

3 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/FastISel.cpp (-8)
  • (modified) llvm/test/CodeGen/X86/stack-protector-msvc-oz.ll (+3-4)
  • (modified) llvm/test/CodeGen/X86/stack-protector-msvc.ll (+3-4)
diff --git a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
index 5c84059da273b..51391f1aeecde 100644
--- a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
@@ -1565,14 +1565,6 @@ bool FastISel::selectInstruction(const Instruction *I) {
 
   if (const auto *Call = dyn_cast<CallInst>(I)) {
     const Function *F = Call->getCalledFunction();
-    LibFunc Func;
-
-    // As a special case, don't handle calls to builtin library functions that
-    // may be translated directly to target instructions.
-    if (F && !F->hasLocalLinkage() && F->hasName() &&
-        LibInfo->getLibFunc(F->getName(), Func) &&
-        LibInfo->hasOptimizedCodeGen(Func))
-      return false;
 
     // Don't handle Intrinsic::trap if a trap function is specified.
     if (F && F->getIntrinsicID() == Intrinsic::trap &&
diff --git a/llvm/test/CodeGen/X86/stack-protector-msvc-oz.ll b/llvm/test/CodeGen/X86/stack-protector-msvc-oz.ll
index d8a772efbd7ed..4ab23cdde74c6 100644
--- a/llvm/test/CodeGen/X86/stack-protector-msvc-oz.ll
+++ b/llvm/test/CodeGen/X86/stack-protector-msvc-oz.ll
@@ -63,11 +63,10 @@ define void @test(ptr %a) nounwind ssp minsize {
 ; MSVC-X86-O0-NEXT:    movl ___security_cookie, %eax
 ; MSVC-X86-O0-NEXT:    xorl %esp, %eax
 ; MSVC-X86-O0-NEXT:    movl %eax, {{[0-9]+}}(%esp)
-; MSVC-X86-O0-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; MSVC-X86-O0-NEXT:    movl %esp, %eax
-; MSVC-X86-O0-NEXT:    movl %ecx, 4(%eax)
+; MSVC-X86-O0-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; MSVC-X86-O0-NEXT:    leal {{[0-9]+}}(%esp), %ecx
-; MSVC-X86-O0-NEXT:    movl %ecx, (%eax)
+; MSVC-X86-O0-NEXT:    movl %ecx, (%esp)
+; MSVC-X86-O0-NEXT:    movl %eax, {{[0-9]+}}(%esp)
 ; MSVC-X86-O0-NEXT:    calll _strcpy
 ; MSVC-X86-O0-NEXT:    leal LC, %ecx
 ; MSVC-X86-O0-NEXT:    leal {{[0-9]+}}(%esp), %eax
diff --git a/llvm/test/CodeGen/X86/stack-protector-msvc.ll b/llvm/test/CodeGen/X86/stack-protector-msvc.ll
index a868fa549296d..3109733e0b0b7 100644
--- a/llvm/test/CodeGen/X86/stack-protector-msvc.ll
+++ b/llvm/test/CodeGen/X86/stack-protector-msvc.ll
@@ -75,11 +75,10 @@ define void @test(ptr %a) nounwind ssp {
 ; MSVC-X86-O0-NEXT:    movl ___security_cookie, %eax
 ; MSVC-X86-O0-NEXT:    xorl %esp, %eax
 ; MSVC-X86-O0-NEXT:    movl %eax, {{[0-9]+}}(%esp)
-; MSVC-X86-O0-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; MSVC-X86-O0-NEXT:    movl %esp, %eax
-; MSVC-X86-O0-NEXT:    movl %ecx, 4(%eax)
+; MSVC-X86-O0-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; MSVC-X86-O0-NEXT:    leal {{[0-9]+}}(%esp), %ecx
-; MSVC-X86-O0-NEXT:    movl %ecx, (%eax)
+; MSVC-X86-O0-NEXT:    movl %ecx, (%esp)
+; MSVC-X86-O0-NEXT:    movl %eax, {{[0-9]+}}(%esp)
 ; MSVC-X86-O0-NEXT:    calll _strcpy
 ; MSVC-X86-O0-NEXT:    leal LC, %ecx
 ; MSVC-X86-O0-NEXT:    leal {{[0-9]+}}(%esp), %eax

@llvmbot
Copy link
Member

llvmbot commented Dec 11, 2025

@llvm/pr-subscribers-llvm-selectiondag

Author: Nikita Popov (nikic)

Changes

The fast instruction selector should should not force an SDAG fallback to potentially make use of optimized libcall implementations.

Looking at 3e6fa46, part of the motivation was to avoid libcalls in unoptimized builds for targets that don't have them, but I believe this should be handled by Clang directly emitting intrinsics instead of libcalls (which it already does). FastISel should not second guess this.

Followup to #171288.


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

3 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/FastISel.cpp (-8)
  • (modified) llvm/test/CodeGen/X86/stack-protector-msvc-oz.ll (+3-4)
  • (modified) llvm/test/CodeGen/X86/stack-protector-msvc.ll (+3-4)
diff --git a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
index 5c84059da273b..51391f1aeecde 100644
--- a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
@@ -1565,14 +1565,6 @@ bool FastISel::selectInstruction(const Instruction *I) {
 
   if (const auto *Call = dyn_cast<CallInst>(I)) {
     const Function *F = Call->getCalledFunction();
-    LibFunc Func;
-
-    // As a special case, don't handle calls to builtin library functions that
-    // may be translated directly to target instructions.
-    if (F && !F->hasLocalLinkage() && F->hasName() &&
-        LibInfo->getLibFunc(F->getName(), Func) &&
-        LibInfo->hasOptimizedCodeGen(Func))
-      return false;
 
     // Don't handle Intrinsic::trap if a trap function is specified.
     if (F && F->getIntrinsicID() == Intrinsic::trap &&
diff --git a/llvm/test/CodeGen/X86/stack-protector-msvc-oz.ll b/llvm/test/CodeGen/X86/stack-protector-msvc-oz.ll
index d8a772efbd7ed..4ab23cdde74c6 100644
--- a/llvm/test/CodeGen/X86/stack-protector-msvc-oz.ll
+++ b/llvm/test/CodeGen/X86/stack-protector-msvc-oz.ll
@@ -63,11 +63,10 @@ define void @test(ptr %a) nounwind ssp minsize {
 ; MSVC-X86-O0-NEXT:    movl ___security_cookie, %eax
 ; MSVC-X86-O0-NEXT:    xorl %esp, %eax
 ; MSVC-X86-O0-NEXT:    movl %eax, {{[0-9]+}}(%esp)
-; MSVC-X86-O0-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; MSVC-X86-O0-NEXT:    movl %esp, %eax
-; MSVC-X86-O0-NEXT:    movl %ecx, 4(%eax)
+; MSVC-X86-O0-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; MSVC-X86-O0-NEXT:    leal {{[0-9]+}}(%esp), %ecx
-; MSVC-X86-O0-NEXT:    movl %ecx, (%eax)
+; MSVC-X86-O0-NEXT:    movl %ecx, (%esp)
+; MSVC-X86-O0-NEXT:    movl %eax, {{[0-9]+}}(%esp)
 ; MSVC-X86-O0-NEXT:    calll _strcpy
 ; MSVC-X86-O0-NEXT:    leal LC, %ecx
 ; MSVC-X86-O0-NEXT:    leal {{[0-9]+}}(%esp), %eax
diff --git a/llvm/test/CodeGen/X86/stack-protector-msvc.ll b/llvm/test/CodeGen/X86/stack-protector-msvc.ll
index a868fa549296d..3109733e0b0b7 100644
--- a/llvm/test/CodeGen/X86/stack-protector-msvc.ll
+++ b/llvm/test/CodeGen/X86/stack-protector-msvc.ll
@@ -75,11 +75,10 @@ define void @test(ptr %a) nounwind ssp {
 ; MSVC-X86-O0-NEXT:    movl ___security_cookie, %eax
 ; MSVC-X86-O0-NEXT:    xorl %esp, %eax
 ; MSVC-X86-O0-NEXT:    movl %eax, {{[0-9]+}}(%esp)
-; MSVC-X86-O0-NEXT:    movl {{[0-9]+}}(%esp), %ecx
-; MSVC-X86-O0-NEXT:    movl %esp, %eax
-; MSVC-X86-O0-NEXT:    movl %ecx, 4(%eax)
+; MSVC-X86-O0-NEXT:    movl {{[0-9]+}}(%esp), %eax
 ; MSVC-X86-O0-NEXT:    leal {{[0-9]+}}(%esp), %ecx
-; MSVC-X86-O0-NEXT:    movl %ecx, (%eax)
+; MSVC-X86-O0-NEXT:    movl %ecx, (%esp)
+; MSVC-X86-O0-NEXT:    movl %eax, {{[0-9]+}}(%esp)
 ; MSVC-X86-O0-NEXT:    calll _strcpy
 ; MSVC-X86-O0-NEXT:    leal LC, %ecx
 ; MSVC-X86-O0-NEXT:    leal {{[0-9]+}}(%esp), %eax

@nikic nikic merged commit d33d80f into llvm:main Dec 11, 2025
13 checks passed
@nikic nikic deleted the fastisel-optimized-codegen branch December 11, 2025 13:14
@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 11, 2025

LLVM Buildbot has detected a new failure on builder ppc64le-mlir-rhel-clang running on ppc64le-mlir-rhel-test while building llvm at step 6 "test-build-check-mlir-build-only-check-mlir".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/129/builds/34828

Here is the relevant piece of the build log for the reference
Step 6 (test-build-check-mlir-build-only-check-mlir) failure: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
...
PASS: MLIR :: mlir-tblgen/op-interface.td (3743 of 3754)
PASS: MLIR :: mlir-tblgen/rewriter-errors.td (3744 of 3754)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/11/22 (3745 of 3754)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/13/22 (3746 of 3754)
PASS: MLIR-Unit :: Pass/./MLIRPassTests/11/14 (3747 of 3754)
PASS: MLIR :: mlir-tblgen/cpp-class-comments.td (3748 of 3754)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/12/22 (3749 of 3754)
PASS: MLIR :: mlir-runner/simple.mlir (3750 of 3754)
PASS: MLIR :: mlir-reduce/dce-test.mlir (3751 of 3754)
PASS: MLIR :: mlir-tblgen/llvm-intrinsics.td (3752 of 3754)
command timed out: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=2127.085042

@nikic
Copy link
Contributor Author

nikic commented Dec 11, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:X86 llvm:SelectionDAG SelectionDAGISel as well

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants