[llvm] [C API] Add getters for Target Extension Types to C API (PR #71291)

Benji Smith via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 6 14:24:11 PST 2023


https://github.com/Benjins updated https://github.com/llvm/llvm-project/pull/71291

>From 12e5ec3c0727d58bf8d91f673c3facd974f98c54 Mon Sep 17 00:00:00 2001
From: Benji Smith <6193112+Benjins at users.noreply.github.com>
Date: Sat, 4 Nov 2023 11:57:20 -0400
Subject: [PATCH 1/2] [C API] Add getters for Target Extension Types to C API

These types were added in LLVM-16, and the C API supports constructing them,
but not getting information back from them

This change adds getters for them to the C API, updates the echo test to be
able to clone Target Extension Types, and adds some usage of them to the
echo.ll test to confirm that they work
---
 llvm/include/llvm-c/Core.h        | 33 +++++++++++++++++++++++++++
 llvm/lib/IR/Core.cpp              | 32 ++++++++++++++++++++++++++
 llvm/test/Bindings/llvm-c/echo.ll | 17 ++++++++++++++
 llvm/tools/llvm-c-test/echo.cpp   | 38 +++++++++++++++++++++++++++++--
 4 files changed, 118 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 4fc88b2b64eacea..2d95f198f64958f 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -1654,6 +1654,39 @@ LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name,
                                        unsigned *IntParams,
                                        unsigned IntParamCount);
 
+/**
+ * Obtain the name for this target extension type
+ */
+const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy);
+
+/**
+ * Obtain the number of type parameters for this target extension type
+ */
+unsigned LLVMCountTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy);
+
+/**
+ * Obtain the values of a target extension type's type parameters, output to
+ * the passed-in pointer. The pointer should have enough space for all type
+ * params for the given target extension type
+ *
+ * @see LLVMCountTargetExtTypeTypeParams
+ */
+void LLVMGetTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy, LLVMTypeRef *Dest);
+
+/**
+ * Obtain the number of int parameters for this target extension type
+ */
+unsigned LLVMCountTargetExtTypeIntParams(LLVMTypeRef TargetExtTy);
+
+/**
+ * Obtain the int values of a target extension type's int parameters, output to
+ * the passed-in pointer. The pointer should have enough space for all int
+ * params for the given target extension type
+ *
+ * @see LLVMCountTargetExtTypeIntParams
+ */
+void LLVMGetTargetExtTypeIntParams(LLVMTypeRef TargetExtTy, unsigned *Dest);
+
 /**
  * @}
  */
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 076d1089582fe7e..eb1cda55288e334 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -924,6 +924,38 @@ LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name,
       TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray));
 }
 
+const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy) {
+  TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
+  return Type->getName().data();
+}
+
+unsigned LLVMCountTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy) {
+  TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
+  return Type->getNumTypeParameters();
+}
+
+void LLVMGetTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy,
+                                    LLVMTypeRef *Dest) {
+  TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
+
+  for (unsigned int i = 0; i < Type->getNumTypeParameters(); i++) {
+    Dest[i] = wrap(Type->getTypeParameter(i));
+  }
+}
+
+unsigned LLVMCountTargetExtTypeIntParams(LLVMTypeRef TargetExtTy) {
+  TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
+  return Type->getNumIntParameters();
+}
+
+void LLVMGetTargetExtTypeIntParams(LLVMTypeRef TargetExtTy, unsigned *Dest) {
+  TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
+
+  for (unsigned int i = 0; i < Type->getNumIntParameters(); i++) {
+    Dest[i] = Type->getIntParameter(i);
+  }
+}
+
 /*===-- Operations on values ----------------------------------------------===*/
 
 /*--.. Operations on all values ............................................--*/
diff --git a/llvm/test/Bindings/llvm-c/echo.ll b/llvm/test/Bindings/llvm-c/echo.ll
index 5daa238bfb8e533..e62670424112d51 100644
--- a/llvm/test/Bindings/llvm-c/echo.ll
+++ b/llvm/test/Bindings/llvm-c/echo.ll
@@ -66,6 +66,23 @@ define void @types() {
   ret void
 }
 
+; Target extension types:
+define target("target.ext.1") @target_ext_01(target("target.ext.1") %0) {
+	ret target("target.ext.1") %0
+}
+
+define target("target.ext.2", i8, i1) @target_ext_02(target("target.ext.2", i8, i1) %0) {
+	ret target("target.ext.2", i8, i1) %0
+}
+
+define target("target.ext.3", 7) @target_ext_03(target("target.ext.3", 7) %0) {
+	ret target("target.ext.3", 7) %0
+}
+
+define target("target.ext.4", i1, i32, 7) @target_ext_04(target("target.ext.4", i1, i32, 7) %0) {
+	ret target("target.ext.4", i1, i32, 7) %0
+}
+
 define i32 @iops(i32 %a, i32 %b) {
   %1 = add i32 %a, %b
   %2 = mul i32 %a, %1
diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp
index 06966ce528eae4d..8ff7bc1ed0bf485 100644
--- a/llvm/tools/llvm-c-test/echo.cpp
+++ b/llvm/tools/llvm-c-test/echo.cpp
@@ -157,8 +157,42 @@ struct TypeCloner {
         return LLVMX86MMXTypeInContext(Ctx);
       case LLVMTokenTypeKind:
         return LLVMTokenTypeInContext(Ctx);
-      case LLVMTargetExtTypeKind:
-        assert(false && "Implement me");
+      case LLVMTargetExtTypeKind: {
+        const char *Name = LLVMGetTargetExtTypeName(Src);
+        unsigned TypeParamCount = LLVMCountTargetExtTypeTypeParams(Src);
+        unsigned IntParamCount = LLVMCountTargetExtTypeIntParams(Src);
+
+        LLVMTypeRef *TypeParams = nullptr;
+        unsigned *IntParams = nullptr;
+
+        // If we have type params, get them from Src and clone them individually
+        if (TypeParamCount > 0) {
+          TypeParams = static_cast<LLVMTypeRef *>(
+              safe_malloc(TypeParamCount * sizeof(LLVMTypeRef)));
+          LLVMGetTargetExtTypeTypeParams(Src, TypeParams);
+
+          for (unsigned i = 0; i < TypeParamCount; i++)
+            TypeParams[i] = Clone(TypeParams[i]);
+        }
+
+        // If we have integer params, get them from Src
+        if (IntParamCount > 0) {
+          IntParams = static_cast<unsigned *>(
+              safe_malloc(IntParamCount * sizeof(unsigned)));
+          LLVMGetTargetExtTypeIntParams(Src, IntParams);
+        }
+
+        LLVMTypeRef TargtExtTy = LLVMTargetExtTypeInContext(
+            Ctx, Name, TypeParams, TypeParamCount, IntParams, IntParamCount);
+
+        if (TypeParams)
+          free(TypeParams);
+
+        if (IntParams)
+          free(IntParams);
+
+        return TargtExtTy;
+      }
     }
 
     fprintf(stderr, "%d is not a supported typekind\n", Kind);

>From 05d1b8c1329a1c6369e38db897e7c7b7e95cc910 Mon Sep 17 00:00:00 2001
From: Benji Smith <6193112+Benjins at users.noreply.github.com>
Date: Mon, 6 Nov 2023 17:23:38 -0500
Subject: [PATCH 2/2] Add C API changes for Target Extension Types to Release
 Notes

---
 llvm/docs/ReleaseNotes.rst | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 23b54e6a37a7451..afe93b6fb500e9a 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -179,6 +179,13 @@ Changes to the C API
   The option structure exposes an additional setting (i.e., the target ABI) and
   provides default values for unspecified settings.
 
+* Added the following getters for accessing the name, type parameters, and
+  integer parameters of Target Extension Types:
+
+  * ``LLVMGetTargetExtTypeName``
+  * ``LLVMCountTargetExtTypeTypeParams`` / ``LLVMGetTargetExtTypeTypeParams``
+  * ``LLVMCountTargetExtTypeIntParams`` / ``LLVMGetTargetExtTypeIntParams``
+
 Changes to the CodeGen infrastructure
 -------------------------------------
 



More information about the llvm-commits mailing list