-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[IR] Allow type change in ValueAsMetadata::handleRAUW #76969
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
[IR] Allow type change in ValueAsMetadata::handleRAUW #76969
Conversation
ValueAsMetadata::handleRAUW is a mechanism to replace all metadata referring to one value by a different value. Relax an assert that used to enforce the old and new value to have the same type. This seems to be a sanity checking assert only, and the implementation actually supports mismatching types. This is motivated by a downstream mechanism where we use poison ValueAsMetadata values to annotate pointee types of opaque pointer function arguments. When replacing one type with a different one to work around DXIL vs LLVM incompatibilities, we need to update type annotations, and handleRAUW is more efficient than creating new MD nodes.
@llvm/pr-subscribers-llvm-ir Author: Jannik Silvanus (jasilvanus) Changes
Relax an assert that used to enforce the old and new value to have the same type. This is motivated by a downstream mechanism where we use poison ValueAsMetadata values to annotate pointee types of opaque pointer function arguments. When replacing one type with a different one to work around DXIL vs LLVM incompatibilities, we need to update type annotations, and handleRAUW is more efficient than creating new MD nodes. Full diff: https://github.com/llvm/llvm-project/pull/76969.diff 1 Files Affected:
diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp
index 515893d079b8cb..bdfbd8829186d9 100644
--- a/llvm/lib/IR/Metadata.cpp
+++ b/llvm/lib/IR/Metadata.cpp
@@ -503,7 +503,7 @@ void ValueAsMetadata::handleRAUW(Value *From, Value *To) {
assert(From && "Expected valid value");
assert(To && "Expected valid value");
assert(From != To && "Expected changed value");
- assert(From->getType() == To->getType() && "Unexpected type change");
+ assert(&From->getContext() == &To->getContext() && "Expected same context");
LLVMContext &Context = From->getType()->getContext();
auto &Store = Context.pImpl->ValuesAsMetadata;
|
I feel like this is well motivated, and as you say there aren't actually any parts of LLVM that rely on ValueAsMetadata types for correctness (AFAIUI). Would I be right in thinking that this only enables use-cases that are directly calling ValueAsMetadata::handleRAUW, because performing a normal RAUW will do the usual type checks? CC @OCHyams and @SLTozer as they've touched this area more recently than me. |
This seems fairly harmless to me, especially as @jmorse points out Would it be possible for you to add unittest showing this in action? Otherwise SGTM. |
Agreed with this being harmless - not all cases of |
Will do.
I considered this, but I think such a constraint would be a bit arbitrary. What about
I'll add a test case, but it'll only exercise usage rather then giving a true motivation. To put our application outlined in the PR description in other words, we are using |
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.
I'll add a test case, but it'll only exercise usage rather then giving a true motivation.
IMO this is fine, it discourages people from breaking behaviours that you're relying on. LGTM.
I think your current approach is fine so no need to adjust but FWIW, though it may be arbitrary, |
ValueAsMetadata::handleRAUW
is a mechanism to replace all metadata referring to one value by a different value.Relax an assert that used to enforce the old and new value to have the same type.
This seems to be a sanity plausibility assert only, as the implementation actually supports mismatching types.
This is motivated by a downstream mechanism where we use poison ValueAsMetadata values to annotate pointee types of opaque pointer function arguments.
When replacing one type with a different one to work around DXIL vs LLVM incompatibilities, we need to update type annotations, and handleRAUW is more efficient than creating new MD nodes.