[llvm] [LTO][Veclib] Fix vector library handling with LTO (PR #170638)

Usha Gupta via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 4 02:45:58 PST 2025


https://github.com/usha1830 created https://github.com/llvm/llvm-project/pull/170638

After this https://github.com/llvm/llvm-project/pull/167996, we observed a crash when using -fveclib=ArmPL -flto -mcpu=neoverse-v1

Program crashes with this error, however it only occurs when -fveclib=ArmPL and -flto are used together
`LLVM ERROR: Possible incorrect use of EVT::getVectorNumElements() for scalable vector. Scalable flag may be dropped, use EVT::getVectorElementCount() instead`

The LTO pipeline was not correctly propagating the vector library setting, leading to incorrect codegen for scalable vectors.

This PR fixes the crash by ensuring that  the vector library option is properly propagated through the LTO pipeline, preventing the crash.


>From 015f1a3d242bcc1d07adcdcbed8cfb0089042bf7 Mon Sep 17 00:00:00 2001
From: Usha Gupta <usha.gupta at arm.com>
Date: Thu, 4 Dec 2025 09:56:50 +0000
Subject: [PATCH] Fix vector library handling with LTO

---
 llvm/lib/LTO/LTOBackend.cpp           | 4 ++--
 llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 2 +-
 llvm/lib/LTO/UpdateCompilerUsed.cpp   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index 93118becedbac..705bdbae4acf7 100644
--- a/llvm/lib/LTO/LTOBackend.cpp
+++ b/llvm/lib/LTO/LTOBackend.cpp
@@ -278,7 +278,7 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
   RegisterPassPlugins(Conf.PassPlugins, PB);
 
   std::unique_ptr<TargetLibraryInfoImpl> TLII(
-      new TargetLibraryInfoImpl(TM->getTargetTriple()));
+      new TargetLibraryInfoImpl(TM->getTargetTriple(), TM->Options.VecLib));
   if (Conf.Freestanding)
     TLII->disableAllFunctions();
   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
@@ -444,7 +444,7 @@ static void codegen(const Config &Conf, TargetMachine *TM,
   // keep the pointer and may use it until their destruction. See #138194.
   {
     legacy::PassManager CodeGenPasses;
-    TargetLibraryInfoImpl TLII(Mod.getTargetTriple());
+    TargetLibraryInfoImpl TLII(Mod.getTargetTriple(), TM->Options.VecLib);
     CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
     // No need to make index available if the module is empty.
     // In theory these passes should not use the index for an empty
diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
index ff94c54ab3e6e..ed26c931d0629 100644
--- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
@@ -249,7 +249,7 @@ static void optimizeModule(Module &TheModule, TargetMachine &TM,
   PassBuilder PB(&TM, PTO, PGOOpt, &PIC);
 
   std::unique_ptr<TargetLibraryInfoImpl> TLII(
-      new TargetLibraryInfoImpl(TM.getTargetTriple()));
+      new TargetLibraryInfoImpl(TM.getTargetTriple(), TM.Options.VecLib));
   if (Freestanding)
     TLII->disableAllFunctions();
   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
diff --git a/llvm/lib/LTO/UpdateCompilerUsed.cpp b/llvm/lib/LTO/UpdateCompilerUsed.cpp
index 1889c2b762ff7..f79a3744afad3 100644
--- a/llvm/lib/LTO/UpdateCompilerUsed.cpp
+++ b/llvm/lib/LTO/UpdateCompilerUsed.cpp
@@ -57,7 +57,7 @@ class PreserveLibCallsAndAsmUsed {
   // same names are added to llvm.compiler.used to prevent them from being
   // deleted by optimizations.
   void initializeLibCalls(const Module &TheModule) {
-    TargetLibraryInfoImpl TLII(TM.getTargetTriple());
+    TargetLibraryInfoImpl TLII(TM.getTargetTriple(), TM.Options.VecLib);
     TargetLibraryInfo TLI(TLII);
 
     // TargetLibraryInfo has info on C runtime library calls on the current



More information about the llvm-commits mailing list