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

[LLVM] Add C API support for handling global object metadata. #104786

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ Changes to the C API
* It is now also possible to run the new pass manager on a single function, by calling
``LLVMRunPassesOnFunction`` instead of ``LLVMRunPasses``.

* Metadata APIs (``LLVMHasMetadata``, ``LLVMGetMetadata``, ``LLVMSetMetadata``) now
support global object inputs.

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

Expand Down
38 changes: 28 additions & 10 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,15 +1062,27 @@ void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
}

int LLVMHasMetadata(LLVMValueRef Inst) {
return unwrap<Instruction>(Inst)->hasMetadata();
int LLVMHasMetadata(LLVMValueRef Ref) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Comments in the header need updating.

Value *Val = unwrap<Value>(Ref);
if (auto I = dyn_cast<Instruction>(Val))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (auto I = dyn_cast<Instruction>(Val))
if (auto *I = dyn_cast<Instruction>(Val))

etc.

return I->hasMetadata();
else if (auto GO = dyn_cast<GlobalObject>(Val))
return GO->hasMetadata();
else
assert(0 && "Expected an instruction or a global object");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert(0 && "Expected an instruction or a global object");
llvm_unreachable("Expected an instruction or a global object");

}

LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
auto *I = unwrap<Instruction>(Inst);
assert(I && "Expected instruction");
if (auto *MD = I->getMetadata(KindID))
return wrap(MetadataAsValue::get(I->getContext(), MD));
LLVMValueRef LLVMGetMetadata(LLVMValueRef Ref, unsigned KindID) {
Value *Val = unwrap<Value>(Ref);
if (auto *I = dyn_cast<Instruction>(Val)) {
if (auto *MD = I->getMetadata(KindID))
return wrap(MetadataAsValue::get(I->getContext(), MD));
} else if (auto *GO = dyn_cast<GlobalObject>(Val)) {
if (auto *MD = GO->getMetadata(KindID))
return wrap(MetadataAsValue::get(GO->getContext(), MD));
} else {
assert(0 && "Expected an instruction or a global object");
}
return nullptr;
}

Expand All @@ -1088,10 +1100,16 @@ static MDNode *extractMDNode(MetadataAsValue *MAV) {
return MDNode::get(MAV->getContext(), MD);
}

void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
void LLVMSetMetadata(LLVMValueRef Ref, unsigned KindID, LLVMValueRef MAV) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, we already have LLVMGlobalSetMetadata (which directly takes LLVMMetadataRef rather than this silly MetadataAsValue dance).

So the alternative to this patch would be to add LLVMGlobalGetMetadata/LLVMGlobalHasMetadata instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I somehow didn't know about that API. It's a shame that the Value metadata API is (partially) covered by two slightly different C APIs... I'll check if I can do what I need with the LLVMGlobal API.

Value *Val = unwrap<Value>(Ref);
MDNode *N = MAV ? extractMDNode(unwrap<MetadataAsValue>(MAV)) : nullptr;

unwrap<Instruction>(Inst)->setMetadata(KindID, N);
if (auto *I = dyn_cast<Instruction>(Val))
I->setMetadata(KindID, N);
else if (auto *GO = dyn_cast<GlobalObject>(Val))
GO->setMetadata(KindID, N);
else
assert(0 && "Expected an instruction or a global object");
}

struct LLVMOpaqueValueMetadataEntry {
Expand Down
14 changes: 12 additions & 2 deletions llvm/tools/llvm-c-test/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,26 @@ int llvm_add_named_metadata_operand(void) {
int llvm_set_metadata(void) {
LLVMBuilderRef Builder = LLVMCreateBuilder();

// This used to trigger an assertion
LLVMModuleRef M = LLVMModuleCreateWithName("Mod");
LLVMTypeRef FT = LLVMFunctionType(LLVMVoidType(), NULL, 0, 0);
LLVMValueRef F = LLVMAddFunction(M, "Fun", FT);
LLVMBasicBlockRef BB = LLVMAppendBasicBlock(F, "Entry");
LLVMPositionBuilderAtEnd(Builder, BB);
LLVMValueRef Return = LLVMBuildRetVoid(Builder);

// This used to trigger an assertion because of MDNode canonicalization
const char Name[] = "kind";
LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0);
LLVMSetMetadata(Return, LLVMGetMDKindID(Name, strlen(Name)),
LLVMMDNode(&Int, 1));

// Test support of global objects (e.g., Function)
assert(!LLVMHasMetadata(F));
LLVMSetMetadata(F, LLVMGetMDKindID(Name, strlen(Name)), LLVMMDNode(&Int, 1));
assert(LLVMHasMetadata(F));

LLVMDisposeBuilder(Builder);
LLVMDeleteInstruction(Return);
LLVMDisposeModule(M);

return 0;
}
Expand Down
Loading