[llvm] [DAGCombiner] Add some very basic folds for ADDRSPACECAST (PR #127733)
Artem Belevich via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 26 11:31:45 PST 2025
================
@@ -16054,6 +16056,25 @@ SDValue DAGCombiner::visitBITCAST(SDNode *N) {
return SDValue();
}
+SDValue DAGCombiner::visitADDRSPACECAST(SDNode *N) {
+ auto *ASCN1 = cast<AddrSpaceCastSDNode>(N);
+
+ if (auto *ASCN2 = dyn_cast<AddrSpaceCastSDNode>(ASCN1->getOperand(0))) {
+ assert(ASCN2->getDestAddressSpace() == ASCN1->getSrcAddressSpace());
+
+ // Fold asc[B -> A](asc[A -> B](x)) -> x
+ if (ASCN1->getDestAddressSpace() == ASCN2->getSrcAddressSpace())
+ return ASCN2->getOperand(0);
+
+ // Fold asc[B -> C](asc[A -> B](x)) -> asc[A -> C](x)
----------------
Artem-B wrote:
My example is hypothetical. I don't think we have actual uses that match that pattern.
The point is that we can't imply AS transitivity implicitly. If LLVM relies on it, it should be documented explicitly, IMO.
Right now I can't tell whether the assumption of AS transitivity by existing code in LLVM is intentional or not. If it's by design, then your patch is fine. If not, then we may need to figure out how to deal with that. Probably we'll need to make transitivity target-dependent. I believe we did discuss describing additional target's AS properties in the past.
Right now LLVM does not say much about AS assumptions. The closest I can find is this; https://llvm.org/docs/LangRef.html#pointer-type
> The semantics of non-zero address spaces are target-specific. ...
This suggests that whether ASC is transitive *is* target specific.
> If an object can be proven accessible through a pointer with a different address space, the access may be modified to use that address space.
This phrase can be interpreted that we're allowed to assume transitivity, in principle, as data in my example *is* accessible, even if the ASC results in a different pointer value.
However, even in that case it requires the ASCs to be "proven accessible", and that in general requires target-specific knowledge. The way I read it, collapsing `local->shared->generic` into just `local->generic` is not allowed, as the source pointer in local AS pointer can't be accessed via shared AS. We could collapse such an invalid transition into a poison value, though.
https://github.com/llvm/llvm-project/pull/127733
More information about the llvm-commits
mailing list