Skip to content

[NFC] Add a new Intrinsics.cpp file for intrinsic code #110078

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

Merged
merged 1 commit into from
Oct 1, 2024

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Sep 26, 2024

Add new file Intrinsics.cpp and move all functions in the Intrinsic namespace to it.

@jurahul jurahul marked this pull request as ready for review September 26, 2024 10:11
@jurahul jurahul requested review from nikic and arsenm September 26, 2024 10:12
@llvmbot
Copy link
Member

llvmbot commented Sep 26, 2024

@llvm/pr-subscribers-llvm-ir

Author: Rahul Joshi (jurahul)

Changes

Add new file Intrinsics.cpp and move lookupLLVMIntrinsicByName to that file.


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

4 Files Affected:

  • (modified) llvm/include/llvm/IR/Intrinsics.h (+1-1)
  • (modified) llvm/lib/IR/CMakeLists.txt (+1)
  • (modified) llvm/lib/IR/IntrinsicInst.cpp (-42)
  • (added) llvm/lib/IR/Intrinsics.cpp (+57)
diff --git a/llvm/include/llvm/IR/Intrinsics.h b/llvm/include/llvm/IR/Intrinsics.h
index 0ec7e47812af44..99dd87d3719640 100644
--- a/llvm/include/llvm/IR/Intrinsics.h
+++ b/llvm/include/llvm/IR/Intrinsics.h
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 //
 // This file defines a set of enums which allow processing of intrinsic
-// functions.  Values of these enum types are returned by
+// functions. Values of these enum types are returned by
 // Function::getIntrinsicID.
 //
 //===----------------------------------------------------------------------===//
diff --git a/llvm/lib/IR/CMakeLists.txt b/llvm/lib/IR/CMakeLists.txt
index e5756940dd5a03..544f4ea9223d0e 100644
--- a/llvm/lib/IR/CMakeLists.txt
+++ b/llvm/lib/IR/CMakeLists.txt
@@ -32,6 +32,7 @@ add_llvm_component_library(LLVMCore
   GCStrategy.cpp
   GVMaterializer.cpp
   Globals.cpp
+  Intrinsics.cpp
   IRBuilder.cpp
   IRPrintingPasses.cpp
   SSAContext.cpp
diff --git a/llvm/lib/IR/IntrinsicInst.cpp b/llvm/lib/IR/IntrinsicInst.cpp
index 5654a3a3236c6d..0a6c93fde6302f 100644
--- a/llvm/lib/IR/IntrinsicInst.cpp
+++ b/llvm/lib/IR/IntrinsicInst.cpp
@@ -236,48 +236,6 @@ void DbgAssignIntrinsic::setValue(Value *V) {
              MetadataAsValue::get(getContext(), ValueAsMetadata::get(V)));
 }
 
-int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
-                                               StringRef Name,
-                                               StringRef Target) {
-  assert(Name.starts_with("llvm.") && "Unexpected intrinsic prefix");
-  assert(Name.drop_front(5).starts_with(Target) && "Unexpected target");
-
-  // Do successive binary searches of the dotted name components. For
-  // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
-  // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
-  // "llvm.gc.experimental.statepoint", and then we will stop as the range is
-  // size 1. During the search, we can skip the prefix that we already know is
-  // identical. By using strncmp we consider names with differing suffixes to
-  // be part of the equal range.
-  size_t CmpEnd = 4; // Skip the "llvm" component.
-  if (!Target.empty())
-    CmpEnd += 1 + Target.size(); // skip the .target component.
-
-  const char *const *Low = NameTable.begin();
-  const char *const *High = NameTable.end();
-  const char *const *LastLow = Low;
-  while (CmpEnd < Name.size() && High - Low > 0) {
-    size_t CmpStart = CmpEnd;
-    CmpEnd = Name.find('.', CmpStart + 1);
-    CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
-    auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
-      return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
-    };
-    LastLow = Low;
-    std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
-  }
-  if (High - Low > 0)
-    LastLow = Low;
-
-  if (LastLow == NameTable.end())
-    return -1;
-  StringRef NameFound = *LastLow;
-  if (Name == NameFound ||
-      (Name.starts_with(NameFound) && Name[NameFound.size()] == '.'))
-    return LastLow - NameTable.begin();
-  return -1;
-}
-
 ConstantInt *InstrProfCntrInstBase::getNumCounters() const {
   if (InstrProfValueProfileInst::classof(this))
     llvm_unreachable("InstrProfValueProfileInst does not have counters!");
diff --git a/llvm/lib/IR/Intrinsics.cpp b/llvm/lib/IR/Intrinsics.cpp
new file mode 100644
index 00000000000000..c1d2ad31897a20
--- /dev/null
+++ b/llvm/lib/IR/Intrinsics.cpp
@@ -0,0 +1,57 @@
+//===-- Intrinsics.cpp - Intrinsic Function Handling ------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements functions required for supporting intrinsic functions.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/Intrinsics.h"
+
+using namespace llvm;
+
+int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
+                                               StringRef Name,
+                                               StringRef Target) {
+  assert(Name.starts_with("llvm.") && "Unexpected intrinsic prefix");
+  assert(Name.drop_front(5).starts_with(Target) && "Unexpected target");
+
+  // Do successive binary searches of the dotted name components. For
+  // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
+  // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
+  // "llvm.gc.experimental.statepoint", and then we will stop as the range is
+  // size 1. During the search, we can skip the prefix that we already know is
+  // identical. By using strncmp we consider names with differing suffixes to
+  // be part of the equal range.
+  size_t CmpEnd = 4; // Skip the "llvm" component.
+  if (!Target.empty())
+    CmpEnd += 1 + Target.size(); // skip the .target component.
+
+  const char *const *Low = NameTable.begin();
+  const char *const *High = NameTable.end();
+  const char *const *LastLow = Low;
+  while (CmpEnd < Name.size() && High - Low > 0) {
+    size_t CmpStart = CmpEnd;
+    CmpEnd = Name.find('.', CmpStart + 1);
+    CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
+    auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
+      return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
+    };
+    LastLow = Low;
+    std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
+  }
+  if (High - Low > 0)
+    LastLow = Low;
+
+  if (LastLow == NameTable.end())
+    return -1;
+  StringRef NameFound = *LastLow;
+  if (Name == NameFound ||
+      (Name.starts_with(NameFound) && Name[NameFound.size()] == '.'))
+    return LastLow - NameTable.begin();
+  return -1;
+}

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

What's the motivation for splitting this into a separate file? Is this just so the name matches the name of the header?

@jurahul
Copy link
Contributor Author

jurahul commented Sep 26, 2024

What's the motivation for splitting this into a separate file? Is this just so the name matches the name of the header?

The motivation for moving lookupLLVMIntrinsicByName out of IntrinsicInst.cpp was that the function does not have anything to do with IntinsicInst or its subclasses and is mainly used from Function.cpp. I could have just moved it to Function.cpp. But Function.cpp also has a bunch of functions in the Intrinsic namespace and they seem enough of them to make a separate cpp file for them. This PR is just seeding the file. Once it goes in, I can move some others to that file as well (mostly all the ones in the Intrinsic namespace.

@jurahul jurahul requested a review from nikic September 26, 2024 13:14
@jurahul
Copy link
Contributor Author

jurahul commented Sep 26, 2024

We could also rename Intrinsics.h to Intrinsic.h to match the namespace, but that may be too much busy work for questionable benefit.

@nikic
Copy link
Contributor

nikic commented Sep 26, 2024

What's the motivation for splitting this into a separate file? Is this just so the name matches the name of the header?

The motivation for moving lookupLLVMIntrinsicByName out of IntrinsicInst.cpp was that the function does not have anything to do with IntinsicInst or its subclasses and is mainly used from Function.cpp. I could have just moved it to Function.cpp. But Function.cpp also has a bunch of functions in the Intrinsic namespace and they seem enough of them to make a separate cpp file for them. This PR is just seeding the file. Once it goes in, I can move some others to that file as well (mostly all the ones in the Intrinsic namespace.

Can you please also move the functions from Function.cpp over in this PR? It's a bit hard to judge the scope otherwise.

@jurahul
Copy link
Contributor Author

jurahul commented Sep 26, 2024

I did look into that, one issue is that Function::lookupIntrinsicID(StringRef Name) uses the intrinsic name table, so I'd have to move that function to the Intrinsic namespace (its a static function). How about I do that first as a separate prep PR, and then revisit this PR to move the rest of them as well?

@jurahul
Copy link
Contributor Author

jurahul commented Sep 26, 2024

I did look into that, one issue is that Function::lookupIntrinsicID(StringRef Name) uses the intrinsic name table, so I'd have to move that function to the Intrinsic namespace (its a static function). How about I do that first as a separate prep PR, and then revisit this PR to move the rest of them as well?

These 2 to be accurate:

static bool isTargetIntrinsic(Intrinsic::ID IID);
static Intrinsic::ID lookupIntrinsicID(StringRef Name);

@jurahul
Copy link
Contributor Author

jurahul commented Sep 26, 2024

I've started #110125

@jurahul jurahul marked this pull request as draft September 26, 2024 20:53
Add new file Intrinsics.cpp and move `lookupLLVMIntrinsicByName`
to that file.
@jurahul jurahul marked this pull request as ready for review September 30, 2024 21:16
@jurahul
Copy link
Contributor Author

jurahul commented Sep 30, 2024

PTAL at the new version, it moves all function from the Intrinisic namespace to the new file.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@jurahul jurahul merged commit 2469d7e into llvm:main Oct 1, 2024
10 checks passed
@jurahul jurahul deleted the intrinsics_cpp branch October 1, 2024 13:55
@llvm-ci
Copy link
Collaborator

llvm-ci commented Oct 2, 2024

LLVM Buildbot has detected a new failure on builder lld-x86_64-win running on as-worker-93 while building llvm at step 7 "test-build-unified-tree-check-all".

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

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'LLVM-Unit :: Support/./SupportTests.exe/35/87' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:C:\a\lld-x86_64-win\build\unittests\Support\.\SupportTests.exe-LLVM-Unit-22932-35-87.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=87 GTEST_SHARD_INDEX=35 C:\a\lld-x86_64-win\build\unittests\Support\.\SupportTests.exe
--

Script:
--
C:\a\lld-x86_64-win\build\unittests\Support\.\SupportTests.exe --gtest_filter=ProgramEnvTest.CreateProcessLongPath
--
C:\a\lld-x86_64-win\llvm-project\llvm\unittests\Support\ProgramTest.cpp(160): error: Expected equality of these values:
  0
  RC
    Which is: -2

C:\a\lld-x86_64-win\llvm-project\llvm\unittests\Support\ProgramTest.cpp(163): error: fs::remove(Twine(LongPath)): did not return errc::success.
error number: 13
error message: permission denied



C:\a\lld-x86_64-win\llvm-project\llvm\unittests\Support\ProgramTest.cpp:160
Expected equality of these values:
  0
  RC
    Which is: -2

C:\a\lld-x86_64-win\llvm-project\llvm\unittests\Support\ProgramTest.cpp:163
fs::remove(Twine(LongPath)): did not return errc::success.
error number: 13
error message: permission denied




********************


Sterling-Augustine pushed a commit to Sterling-Augustine/llvm-project that referenced this pull request Oct 3, 2024
Add new file Intrinsics.cpp and move all functions in the `Intrinsic`
namespace to it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants