Skip to content

Commit 815f00a

Browse files
committed
Add support for extracting a Value from a ValueAsMetadata.
1 parent 47a7a38 commit 815f00a

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

deps/LLVMExtra/include/LLVMExtra.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ LLVMValueRef LLVMBuildCallWithOpBundle(LLVMBuilderRef B, LLVMValueRef Fn,
153153
LLVMValueRef *Args, unsigned NumArgs,
154154
LLVMOperandBundleDefRef *Bundles, unsigned NumBundles,
155155
const char *Name);
156+
LLVMValueRef LLVMMetadataAsValue2(LLVMContextRef C, LLVMMetadataRef Metadata);
156157

157158
LLVM_C_EXTERN_C_END
158159
#endif

deps/LLVMExtra/lib/llvm-api.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,11 @@ LLVMValueRef LLVMBuildCallWithOpBundle(LLVMBuilderRef B, LLVMValueRef Fn,
518518
return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn), makeArrayRef(unwrap(Args), NumArgs),
519519
BundleArray, Name));
520520
}
521+
522+
LLVMValueRef LLVMMetadataAsValue2(LLVMContextRef C, LLVMMetadataRef Metadata) {
523+
auto *MD = unwrap(Metadata);
524+
if (auto *VAM = dyn_cast<ValueAsMetadata>(MD))
525+
return wrap(VAM->getValue());
526+
else
527+
return wrap(MetadataAsValue::get(*unwrap(C), MD));
528+
}

lib/libLLVM_extra.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,6 @@ function LLVMBuildCallWithOpBundle(B, Fn, Args, NumArgs, Bundles, NumBundles, Na
395395
ccall((:LLVMBuildCallWithOpBundle, libLLVMExtra), LLVMValueRef, (LLVMBuilderRef, LLVMValueRef, Ptr{LLVMValueRef}, Cuint, Ptr{LLVMOperandBundleDefRef}, Cuint, Cstring), B, Fn, Args, NumArgs, Bundles, NumBundles, Name)
396396
end
397397

398+
function LLVMMetadataAsValue2(C, MD)
399+
ccall((:LLVMMetadataAsValue2, libLLVMExtra), LLVMValueRef, (LLVMContextRef, LLVMMetadataRef), C, MD)
400+
end

src/core/metadata.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ end
4848
end
4949
register(MetadataAsValue, API.LLVMMetadataAsValueValueKind)
5050

51-
Value(md::Metadata; ctx::Context) = MetadataAsValue(API.LLVMMetadataAsValue(ctx, md))
51+
# NOTE: this can be used to both pack e.g. metadata as a value, and to extract the
52+
# value from an ValueAsMetadata, so we don't type-assert narrowly here
53+
Value(md::Metadata; ctx::Context) = Value(API.LLVMMetadataAsValue2(ctx, md))
54+
55+
Base.convert(T::Type{<:Value}, val::Metadata) = Value(val)::T
5256

5357
# NOTE: we can't do this automatically, as we can't query the context of metadata...
5458
# add wrappers to do so? would also simplify, e.g., `string(::MDString)`
@@ -72,7 +76,7 @@ register(LocalAsMetadata, API.LLVMLocalAsMetadataMetadataKind)
7276
# metadata from an MetadataAsValue, so we don't type-assert narrowly here
7377
Metadata(val::Value) = Metadata(API.LLVMValueAsMetadata(val))
7478

75-
Base.convert(::Type{Metadata}, val::Value) = Metadata(val)
79+
Base.convert(T::Type{<:Metadata}, val::Value) = Metadata(val)::T
7680

7781

7882
## values

test/core.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,33 @@ end
779779
Context() do ctx
780780
str = MDString("foo"; ctx)
781781
@test string(str) == "foo"
782+
783+
# wrap as Value
784+
val = Value(str; ctx)
785+
@test val isa LLVM.MetadataAsValue
786+
787+
# back to Metadata
788+
md = Metadata(val)
789+
@test md == str
790+
791+
# more specific conversion
792+
@test convert(MDString, val) == str
793+
end
794+
795+
Context() do ctx
796+
int = ConstantInt(42; ctx)
797+
@test convert(Int, int) == 42
798+
799+
# wrap as Metadata
800+
md = Metadata(int)
801+
@test md isa LLVM.ValueAsMetadata
802+
803+
# back to Value
804+
val = Value(md; ctx)
805+
@test val == int
806+
807+
# more specific conversion
808+
@test convert(ConstantInt, val) == int
782809
end
783810

784811
Context() do ctx

0 commit comments

Comments
 (0)