Skip to content

[LLVM][TableGen] Check name conflicts between target dep and independent intrinsics #109826

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
Sep 25, 2024

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Sep 24, 2024

Validate that for target independent intrinsics the second dotted component of their name (after the llvm.) does not match any existing target names (for which atleast one intrinsic has been defined). Doing so is invalid as LLVM will search for that intrinsic in that target's intrinsic table and not find it, and conclude that its an unknown intrinsic.

@jurahul jurahul changed the title [LLVM][TableGen] Add additional target independent intrinsic checks [LLVM][TableGen] Check target independent intrinsic name conflict with target dependent intrinsics Sep 24, 2024
@jurahul jurahul changed the title [LLVM][TableGen] Check target independent intrinsic name conflict with target dependent intrinsics [LLVM][TableGen] Check name conflicts between target dep and independent intrinsics Sep 24, 2024
@jurahul jurahul marked this pull request as ready for review September 24, 2024 19:22
@jurahul jurahul requested review from nikic and arsenm September 24, 2024 19:22
@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2024

@llvm/pr-subscribers-tablegen

Author: Rahul Joshi (jurahul)

Changes

Validate that for target independent intrinsics the second dotted component of their name (after the llvm.) does not match any existing target names (for which atleast one intrinsic has been defined). Doing so is invalid as LLVM will search for that intrinsic in that target's intrinsic table and not find it, and conclude that its an unknown intrinsic.


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

3 Files Affected:

  • (added) llvm/test/TableGen/intrinsic-target-prefix-for-target-independent.td (+9)
  • (modified) llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp (+23)
  • (modified) llvm/utils/TableGen/Basic/CodeGenIntrinsics.h (+1)
diff --git a/llvm/test/TableGen/intrinsic-target-prefix-for-target-independent.td b/llvm/test/TableGen/intrinsic-target-prefix-for-target-independent.td
new file mode 100644
index 00000000000000..157b9490439fca
--- /dev/null
+++ b/llvm/test/TableGen/intrinsic-target-prefix-for-target-independent.td
@@ -0,0 +1,9 @@
+// RUN: not llvm-tblgen -gen-intrinsic-enums -I %p/../../include %s 2>&1 | FileCheck %s -DFILE=%s
+
+include "llvm/IR/Intrinsics.td"
+
+// Check that target independent intrinsics with a prefix that matches a target
+// name are flagged.
+// CHECK: [[FILE]]:[[@LINE+1]]:5: error: target independent intrinsic `llvm.aarch64.foo' has prefix `llvm.aarch64` that conflicts with target intrinsics for target `aarch64`
+def int_aarch64_foo : Intrinsic<[],[]>;
+
diff --git a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
index a30dc72a83154a..b727795cb74db5 100644
--- a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
+++ b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
@@ -75,6 +75,7 @@ CodeGenIntrinsicTable::CodeGenIntrinsicTable(const RecordKeeper &RC) {
   Targets.back().Count = Intrinsics.size() - Targets.back().Offset;
 
   CheckDuplicateIntrinsics();
+  CheckTargetIndependentIntrinsics();
 }
 
 // Check for duplicate intrinsic names.
@@ -101,6 +102,28 @@ void CodeGenIntrinsicTable::CheckDuplicateIntrinsics() const {
   PrintFatalNote(First.TheDef, "Previous definition here");
 }
 
+// For target independent intrinsics, check that their second dotted component
+// does not match any target name.
+void CodeGenIntrinsicTable::CheckTargetIndependentIntrinsics() const {
+  SmallDenseSet<StringRef> TargetNames;
+  for (const auto &Target : ArrayRef(Targets).drop_front())
+    TargetNames.insert(Target.Name);
+
+  // Set of target independent intrinsics.
+  const auto &Set = Targets[0];
+  for (const auto &Int : ArrayRef(&Intrinsics[Set.Offset], Set.Count)) {
+    StringRef Name = Int.Name;
+    StringRef Prefix = Name.drop_front(5).split('.').first;
+    if (!TargetNames.contains(Prefix))
+      continue;
+    PrintFatalError(Int.TheDef,
+                    "target independent intrinsic `" + Name +
+                        "' has prefix `llvm." + Prefix +
+                        "` that conflicts with target intrinsics for target `" +
+                        Prefix + "`");
+  }
+}
+
 CodeGenIntrinsic &CodeGenIntrinsicMap::operator[](const Record *Record) {
   if (!Record->isSubClassOf("Intrinsic"))
     PrintFatalError("Intrinsic defs should be subclass of 'Intrinsic' class");
diff --git a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.h b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.h
index 2df598da3f2507..1cdeaacd52dcdb 100644
--- a/llvm/utils/TableGen/Basic/CodeGenIntrinsics.h
+++ b/llvm/utils/TableGen/Basic/CodeGenIntrinsics.h
@@ -192,6 +192,7 @@ class CodeGenIntrinsicTable {
 
 private:
   void CheckDuplicateIntrinsics() const;
+  void CheckTargetIndependentIntrinsics() const;
 };
 
 // This class builds `CodeGenIntrinsic` on demand for a given Def.

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

This conceptually isn't ideal, but probably isn't a practical concern

@jurahul
Copy link
Contributor Author

jurahul commented Sep 25, 2024

This conceptually isn't ideal, but probably isn't a practical concern

WDYM? We could fall back to looking up in the target independent list if the lookup in the target specific one fails? And this case will actually work as expected? If so, yeah, I agree it's possible, but no one is asking for that.

@arsenm
Copy link
Contributor

arsenm commented Sep 25, 2024

WDYM? We could fall back to looking up in the target independent list if the lookup in the target specific one fails? And this case will actually work as expected? If so, yeah, I agree it's possible, but no one is asking for that.

It's restricting the set of possible intrinsic names. It just happens that it's unlikely that a real operation name doesn't conflict with a target name

@jurahul jurahul force-pushed the check_target_independent_intrinsics branch from e9a62f9 to 606d1d6 Compare September 25, 2024 12:42
Validate that for target independent intrinsics the second dotted
component of their name (after the `llvm.`) does not match any existing
target names (for which atleast one intrinsic has been defined). Doing
so is invalid as LLVM will search for that intrinsic in that target's
intrinsic table and not find it, and conclude that its an unknown
intrinsic.
@jurahul jurahul force-pushed the check_target_independent_intrinsics branch from c4cfc46 to 5713cb6 Compare September 25, 2024 16:55
@jurahul jurahul merged commit 2f43e65 into llvm:main Sep 25, 2024
8 checks passed
@jurahul jurahul deleted the check_target_independent_intrinsics branch September 25, 2024 19:01
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.

3 participants