Skip to content

opt: Add -enable-builtin flag #145808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Jun 25, 2025

Currently TargetLibraryInfo assumes ~everything is available, and
specific triples (or the -disable-builtin) flag, opt-out. This is a
backwards system, where features should only be positively be enabled
when known. Add this flag to help migrate tests with no triple.

Currently TargetLibraryInfo assumes ~everything is available, and
specific triples (or the -disable-builtin) flag, opt-out. This is a
backwards system, where features should only be positively be enabled
when known. Add this flag to help migrate tests with no triple.
Copy link
Contributor Author

arsenm commented Jun 25, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

Currently TargetLibraryInfo assumes ~everything is available, and
specific triples (or the -disable-builtin) flag, opt-out. This is a
backwards system, where features should only be positively be enabled
when known. Add this flag to help migrate tests with no triple.


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

2 Files Affected:

  • (added) llvm/test/Transforms/InferFunctionAttrs/enable-builtin.ll (+13)
  • (modified) llvm/tools/opt/optdriver.cpp (+16-1)
diff --git a/llvm/test/Transforms/InferFunctionAttrs/enable-builtin.ll b/llvm/test/Transforms/InferFunctionAttrs/enable-builtin.ll
new file mode 100644
index 0000000000000..5c1d6ab377497
--- /dev/null
+++ b/llvm/test/Transforms/InferFunctionAttrs/enable-builtin.ll
@@ -0,0 +1,13 @@
+; RUN: opt -S -mtriple=amdgcn-- -passes=inferattrs %s | FileCheck -check-prefix=NOBUILTIN %s
+; RUN: opt -S -enable-builtin=malloc -mtriple=amdgcn-- -passes=inferattrs %s | FileCheck -check-prefix=WITHBUILTIN %s
+
+; Test that the -enable-builtin flag works and forces recognition of
+; malloc despite the target's default TargetLibraryInfo.
+
+; NOBUILTIN: declare ptr @malloc(i64)
+
+; WITHBUILTIN: declare noalias noundef ptr @malloc(i64 noundef) #0
+; WITHBUILTIN: attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
+
+declare ptr @malloc(i64)
+
diff --git a/llvm/tools/opt/optdriver.cpp b/llvm/tools/opt/optdriver.cpp
index de46efa13025d..892b63b5be251 100644
--- a/llvm/tools/opt/optdriver.cpp
+++ b/llvm/tools/opt/optdriver.cpp
@@ -203,6 +203,10 @@ static cl::list<std::string> DisableBuiltins(
     "disable-builtin",
     cl::desc("Disable specific target library builtin function"));
 
+static cl::list<std::string> EnableBuiltins(
+    "enable-builtin",
+    cl::desc("Enable specific target library builtin functions"));
+
 static cl::opt<bool> EnableDebugify(
     "enable-debugify",
     cl::desc(
@@ -670,7 +674,7 @@ extern "C" int optMain(
   else {
     // Disable individual builtin functions in TargetLibraryInfo.
     LibFunc F;
-    for (auto &FuncName : DisableBuiltins)
+    for (auto &FuncName : DisableBuiltins) {
       if (TLII.getLibFunc(FuncName, F))
         TLII.setUnavailable(F);
       else {
@@ -678,6 +682,17 @@ extern "C" int optMain(
                << FuncName << '\n';
         return 1;
       }
+    }
+
+    for (auto &FuncName : EnableBuiltins) {
+      if (TLII.getLibFunc(FuncName, F))
+        TLII.setAvailable(F);
+      else {
+        errs() << argv[0] << ": cannot enable nonexistent builtin function "
+               << FuncName << '\n';
+        return 1;
+      }
+    }
   }
 
   if (UseNPM) {

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

Successfully merging this pull request may close these issues.

2 participants