Skip to content

Commit

Permalink
[C API] Add getters for Target Extension Types to C API (llvm#96447)
Browse files Browse the repository at this point in the history
Accessors for the name, type parameters, and integer parameters are
added. A test is added to echo.ll

This was originally done in
llvm#71291 but that has been stale
for several months. This re-applies the changes, but with some tweaks.
e.g. removing the bulk getters in favour of a simple get-by-index
approach for the type/integer parameters. The latter is more in line
with the rest of the API
  • Loading branch information
Benjins authored and AlexisPerry committed Jun 27, 2024
1 parent d653470 commit 6b9fb9b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
6 changes: 6 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ They are described in detail in the `debug info migration guide <https://llvm.or
* ``LLVMDIBuilderInsertDbgValueBefore``
* ``LLVMDIBuilderInsertDbgValueAtEnd``

* Added the following functions for accessing a Target Extension Type's data:

* ``LLVMGetTargetExtTypeName``
* ``LLVMGetTargetExtTypeNumTypeParams``/``LLVMGetTargetExtTypeTypeParam``
* ``LLVMGetTargetExtTypeNumIntParams``/``LLVMGetTargetExtTypeIntParam``

Changes to the CodeGen infrastructure
-------------------------------------

Expand Down
36 changes: 36 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,42 @@ LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name,
unsigned *IntParams,
unsigned IntParamCount);

/**
* Obtain the name for this target extension type.
*
* @see llvm::TargetExtType::getName()
*/
const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy);

/**
* Obtain the number of type parameters for this target extension type.
*
* @see llvm::TargetExtType::getNumTypeParameters()
*/
unsigned LLVMGetTargetExtTypeNumTypeParams(LLVMTypeRef TargetExtTy);

/**
* Get the type parameter at the given index for the target extension type.
*
* @see llvm::TargetExtType::getTypeParameter()
*/
LLVMTypeRef LLVMGetTargetExtTypeTypeParam(LLVMTypeRef TargetExtTy,
unsigned Idx);

/**
* Obtain the number of int parameters for this target extension type.
*
* @see llvm::TargetExtType::getNumIntParameters()
*/
unsigned LLVMGetTargetExtTypeNumIntParams(LLVMTypeRef TargetExtTy);

/**
* Get the int parameter at the given index for the target extension type.
*
* @see llvm::TargetExtType::getIntParameter()
*/
unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx);

/**
* @}
*/
Expand Down
26 changes: 26 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,32 @@ 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 LLVMGetTargetExtTypeNumTypeParams(LLVMTypeRef TargetExtTy) {
TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
return Type->getNumTypeParameters();
}

LLVMTypeRef LLVMGetTargetExtTypeTypeParam(LLVMTypeRef TargetExtTy,
unsigned Idx) {
TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
return wrap(Type->getTypeParameter(Idx));
}

unsigned LLVMGetTargetExtTypeNumIntParams(LLVMTypeRef TargetExtTy) {
TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
return Type->getNumIntParameters();
}

unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx) {
TargetExtType *Type = unwrap<TargetExtType>(TargetExtTy);
return Type->getIntParameter(Idx);
}

/*===-- Operations on values ----------------------------------------------===*/

/*--.. Operations on all values ............................................--*/
Expand Down
17 changes: 17 additions & 0 deletions llvm/test/Bindings/llvm-c/echo.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions llvm/tools/llvm-c-test/echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 NumTypeParams = LLVMGetTargetExtTypeNumTypeParams(Src);
unsigned NumIntParams = LLVMGetTargetExtTypeNumIntParams(Src);

SmallVector<LLVMTypeRef, 4> TypeParams((size_t)NumTypeParams);
SmallVector<unsigned, 4> IntParams((size_t)NumIntParams);

for (unsigned i = 0; i < TypeParams.size(); i++)
TypeParams[i] = Clone(LLVMGetTargetExtTypeTypeParam(Src, i));

for (unsigned i = 0; i < IntParams.size(); i++)
IntParams[i] = LLVMGetTargetExtTypeIntParam(Src, i);

LLVMTypeRef TargetExtTy = LLVMTargetExtTypeInContext(
Ctx, Name, TypeParams.data(), TypeParams.size(), IntParams.data(),
IntParams.size());

return TargetExtTy;
}
}

fprintf(stderr, "%d is not a supported typekind\n", Kind);
Expand Down

0 comments on commit 6b9fb9b

Please sign in to comment.