diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst index 2c160f1707cbb9..1e96ed005869f0 100644 --- a/llvm/docs/ReleaseNotes.rst +++ b/llvm/docs/ReleaseNotes.rst @@ -202,6 +202,18 @@ Changes to the C API * Added ``LLVMGetNNeg`` and ``LLVMSetNNeg`` for setting/getting the new nneg flag on zext instructions +* Added the following getters for accessing the name, type parameters, and + integer parameters of Target Extension Types: + + * ``LLVMGetTargetExtTypeName`` + * ``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 b16f67ef02f336..a930f7b8471250 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -1653,6 +1653,50 @@ 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); + +/** + * 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 + */ +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); + +/** + * 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 e07664f8a17c6d..1cc22d117a5442 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -924,6 +924,49 @@ 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)); + } +} + +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(); +} + +void LLVMGetTargetExtTypeIntParams(LLVMTypeRef TargetExtTy, unsigned *Dest) { + TargetExtType *Type = unwrap(TargetExtTy); + + for (unsigned int i = 0; i < Type->getNumIntParameters(); i++) { + Dest[i] = Type->getIntParameter(i); + } +} + +unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx) { + TargetExtType *Type = unwrap(TargetExtTy); + return Type->getIntParameter(Idx); +} + /*===-- 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 72d5b455badcbe..3e96e0b6f52d2b 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 3b07ccb29f3e06..31426bbae28e17 100644 --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -157,8 +157,26 @@ 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); + + SmallVector TypeParams((size_t)TypeParamCount); + SmallVector IntParams((size_t)IntParamCount); + + LLVMGetTargetExtTypeTypeParams(Src, TypeParams.data()); + for (unsigned i = 0; i < TypeParams.size(); i++) + TypeParams[i] = Clone(TypeParams[i]); + + LLVMGetTargetExtTypeIntParams(Src, IntParams.data()); + + LLVMTypeRef TargtExtTy = LLVMTargetExtTypeInContext( + Ctx, Name, TypeParams.data(), TypeParams.size(), IntParams.data(), + IntParams.size()); + + return TargtExtTy; + } } fprintf(stderr, "%d is not a supported typekind\n", Kind);