-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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) { | ||||||
Value *Val = unwrap<Value>(Ref); | ||||||
if (auto I = dyn_cast<Instruction>(Val)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
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; | ||||||
} | ||||||
|
||||||
|
@@ -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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
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 { | ||||||
|
There was a problem hiding this comment.
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.