Skip to content
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

[C API] Add getters for Target Extension Types to C API #71291

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------------------------

Expand Down
33 changes: 33 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
* @}
*/
Expand Down
32 changes: 32 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ............................................--*/
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
38 changes: 36 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,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;
Copy link
Contributor

Choose a reason for hiding this comment

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

These types should be SmallVector<LLVMTypeRef, 4> and SmallVector<unsigned, 4>, respectively...


// 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);
Expand Down