From 12e5ec3c0727d58bf8d91f673c3facd974f98c54 Mon Sep 17 00:00:00 2001 From: Benji Smith <6193112+Benjins@users.noreply.github.com> Date: Sat, 4 Nov 2023 11:57:20 -0400 Subject: [PATCH 1/4] [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 4fc88b2b64eace..2d95f198f64958 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 076d1089582fe7..eb1cda55288e33 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(TargetExtTy); + return Type->getName().data(); +} + +unsigned LLVMCountTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy) { + TargetExtType *Type = unwrap(TargetExtTy); + return Type->getNumTypeParameters(); +} + +void LLVMGetTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy, + LLVMTypeRef *Dest) { + TargetExtType *Type = unwrap(TargetExtTy); + + for (unsigned int i = 0; i < Type->getNumTypeParameters(); i++) { + Dest[i] = wrap(Type->getTypeParameter(i)); + } +} + +unsigned LLVMCountTargetExtTypeIntParams(LLVMTypeRef TargetExtTy) { + TargetExtType *Type = unwrap(TargetExtTy); + return Type->getNumIntParameters(); +} + +void LLVMGetTargetExtTypeIntParams(LLVMTypeRef TargetExtTy, unsigned *Dest) { + TargetExtType *Type = unwrap(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 5daa238bfb8e53..e62670424112d5 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 06966ce528eae4..8ff7bc1ed0bf48 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( + 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( + 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@users.noreply.github.com> Date: Mon, 6 Nov 2023 17:23:38 -0500 Subject: [PATCH 2/4] 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 23b54e6a37a745..afe93b6fb500e9 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 ------------------------------------- From b2112799386fc9ec868c9ea91d5b3a8cc74a83d3 Mon Sep 17 00:00:00 2001 From: Benji Smith <6193112+Benjins@users.noreply.github.com> Date: Mon, 20 Nov 2023 18:01:42 -0500 Subject: [PATCH 3/4] Refactor Target Extension Type cloning in echo.cpp to use SmallVector instead of raw pointers --- llvm/tools/llvm-c-test/echo.cpp | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp index 8ff7bc1ed0bf48..ab94564917ee1b 100644 --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -162,34 +162,18 @@ struct TypeCloner { unsigned TypeParamCount = LLVMCountTargetExtTypeTypeParams(Src); unsigned IntParamCount = LLVMCountTargetExtTypeIntParams(Src); - LLVMTypeRef *TypeParams = nullptr; - unsigned *IntParams = nullptr; + SmallVector TypeParams((size_t)TypeParamCount); + SmallVector IntParams((size_t)IntParamCount); - // If we have type params, get them from Src and clone them individually - if (TypeParamCount > 0) { - TypeParams = static_cast( - safe_malloc(TypeParamCount * sizeof(LLVMTypeRef))); - LLVMGetTargetExtTypeTypeParams(Src, TypeParams); + LLVMGetTargetExtTypeTypeParams(Src, TypeParams.data()); + for (unsigned i = 0; i < TypeParams.size(); i++) + TypeParams[i] = Clone(TypeParams[i]); - 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( - safe_malloc(IntParamCount * sizeof(unsigned))); - LLVMGetTargetExtTypeIntParams(Src, IntParams); - } + LLVMGetTargetExtTypeIntParams(Src, IntParams.data()); LLVMTypeRef TargtExtTy = LLVMTargetExtTypeInContext( - Ctx, Name, TypeParams, TypeParamCount, IntParams, IntParamCount); - - if (TypeParams) - free(TypeParams); - - if (IntParams) - free(IntParams); + Ctx, Name, TypeParams.data(), TypeParams.size(), IntParams.data(), + IntParams.size()); return TargtExtTy; } From 5bd2b4b80e74f55134c3cf5e8e8d9622d9f32214 Mon Sep 17 00:00:00 2001 From: Benji Smith <6193112+Benjins@users.noreply.github.com> Date: Mon, 20 Nov 2023 18:15:30 -0500 Subject: [PATCH 4/4] Add LLVMGetTargetExtTypeTypeParam and LLVMGetTargetExtTypeIntParam accessors --- llvm/docs/ReleaseNotes.rst | 8 ++++++-- llvm/include/llvm-c/Core.h | 11 +++++++++++ llvm/lib/IR/Core.cpp | 11 +++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst index afe93b6fb500e9..df8fd3c36705b8 100644 --- a/llvm/docs/ReleaseNotes.rst +++ b/llvm/docs/ReleaseNotes.rst @@ -183,8 +183,12 @@ Changes to the C API integer parameters of Target Extension Types: * ``LLVMGetTargetExtTypeName`` - * ``LLVMCountTargetExtTypeTypeParams`` / ``LLVMGetTargetExtTypeTypeParams`` - * ``LLVMCountTargetExtTypeIntParams`` / ``LLVMGetTargetExtTypeIntParams`` + * ``LLVMCountTargetExtTypeTypeParams`` + * ``LLVMGetTargetExtTypeTypeParams`` + * ``LLVMGetTargetExtTypeTypeParam`` + * ``LLVMCountTargetExtTypeIntParams`` + * ``LLVMGetTargetExtTypeIntParams`` + * ``LLVMGetTargetExtTypeIntParam`` Changes to the CodeGen infrastructure ------------------------------------- diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index 2d95f198f64958..c265bd46fc4613 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -1673,6 +1673,12 @@ unsigned LLVMCountTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy); */ void LLVMGetTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy, LLVMTypeRef *Dest); +/** + * Get the type param at the given index for the target extension type + */ +LLVMTypeRef LLVMGetTargetExtTypeTypeParam(LLVMTypeRef TargetExtTy, + unsigned Idx); + /** * Obtain the number of int parameters for this target extension type */ @@ -1687,6 +1693,11 @@ unsigned LLVMCountTargetExtTypeIntParams(LLVMTypeRef TargetExtTy); */ void LLVMGetTargetExtTypeIntParams(LLVMTypeRef TargetExtTy, unsigned *Dest); +/** + * Get the int param at the given index for the target extension type + */ +unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx); + /** * @} */ diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index eb1cda55288e33..d2cafa068738ab 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -943,6 +943,12 @@ void LLVMGetTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy, } } +LLVMTypeRef LLVMGetTargetExtTypeTypeParam(LLVMTypeRef TargetExtTy, + unsigned Idx) { + TargetExtType *Type = unwrap(TargetExtTy); + return wrap(Type->getTypeParameter(Idx)); +} + unsigned LLVMCountTargetExtTypeIntParams(LLVMTypeRef TargetExtTy) { TargetExtType *Type = unwrap(TargetExtTy); return Type->getNumIntParameters(); @@ -956,6 +962,11 @@ void LLVMGetTargetExtTypeIntParams(LLVMTypeRef TargetExtTy, unsigned *Dest) { } } +unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx) { + TargetExtType *Type = unwrap(TargetExtTy); + return Type->getIntParameter(Idx); +} + /*===-- Operations on values ----------------------------------------------===*/ /*--.. Operations on all values ............................................--*/