Skip to content

Commit 58a8e6b

Browse files
authored
Merge pull request #302 from maleadt/tb/extract_mav
Add support for extracting a Value from a ValueAsMetadata.
2 parents 47a7a38 + f42ba4a commit 58a8e6b

File tree

7 files changed

+51
-8
lines changed

7 files changed

+51
-8
lines changed

Manifest.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
3333
version = "1.4.1"
3434

3535
[[LLVMExtra_jll]]
36-
deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl", "Pkg"]
37-
git-tree-sha1 = "5558ad3c8972d602451efe9d81c78ec14ef4f5ef"
36+
deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl", "Pkg", "TOML"]
37+
git-tree-sha1 = "00d23b26d194507028b9a1c2728a691ab9914262"
3838
uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
39-
version = "0.0.14+2"
39+
version = "0.0.15+0"
4040

4141
[[LazyArtifacts]]
4242
deps = ["Artifacts", "Pkg"]
@@ -84,9 +84,9 @@ uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
8484

8585
[[Preferences]]
8686
deps = ["TOML"]
87-
git-tree-sha1 = "de893592a221142f3db370f48290e3a2ef39998f"
87+
git-tree-sha1 = "47e5f437cc0e7ef2ce8406ce1e7e24d44915f88d"
8888
uuid = "21216c6a-2e73-6563-6e65-726566657250"
89-
version = "1.2.4"
89+
version = "1.3.0"
9090

9191
[[Printf]]
9292
deps = ["Unicode"]

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
1111

1212
[compat]
1313
CEnum = "0.2, 0.3, 0.4"
14-
LLVMExtra_jll = "=0.0.14"
14+
LLVMExtra_jll = "=0.0.15"
1515
julia = "1.6"

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)