Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,12 @@ static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
ID.AddInteger(M);
break;
}
case ISD::ADDRSPACECAST: {
const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(N);
ID.AddInteger(ASC->getSrcAddressSpace());
ID.AddInteger(ASC->getDestAddressSpace());
break;
}
case ISD::TargetBlockAddress:
case ISD::BlockAddress: {
const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
Expand Down
19 changes: 19 additions & 0 deletions llvm/test/CodeGen/NVPTX/addrspacecast-cse.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
; RUN: llc < %s -mcpu=sm_80 -mattr=+ptx73 -debug-only=isel -o /dev/null 2>&1 | FileCheck %s

; REQUIRES: asserts

target triple = "nvptx64-nvidia-cuda"

;; Selection DAG CSE is hard to test since we run CSE/GVN on the IR before and
;; after selection DAG ISel so most cases will be handled by one of these.
Comment on lines +7 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also hack around this with -O0, the CSE always happens at any opt level (a dirtier hack would be use -start-before/after). I'm not sure I understand how stackrestore helps here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to get around the pre-SelectionDAG CSE by using the stackrestore which causes a addrspacecast to be added during DAG legalization, so that isn't a problem. However it seems like there is some post-ISel (perhaps DAG-CSE on the machine nodes?) which is preventing the impact of this change from being easily observed in the final output. That's why I'm using the debug output.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-stop-after=finalize-isel might work for avoiding machine cse

define void @foo(ptr %p) {
; CHECK-LABEL: Optimized legalized selection DAG: %bb.0 'foo:'
; CHECK: addrspacecast[0 -> 5]
; CHECK-NOT: addrspacecast[0 -> 5]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-NOT checks, especially of debug print scraping are fragile and should be avoided, or as loose as possible. These stop being useful if the debug format ever changes slightly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup.

For the negative checks, the best compromise I've found is to run them as a separate test "RUN". At least that way it's possible to correctly set the bounds on the areas where the negative checks should apply. Mixing them with the regular checks is hard to implement correctly in general.

That said, in this particular case the intent appears to be to verify that there's only one instance of addrspacecast, and this combination of check and check-not is reasonable. We could use CHECK-COUNT-1 to make it more obvious what we're doing here.

An alternative would be to autogenerate the checks and just match the exact output, but that's probably not going to work all that well with matching unstable debug printouts.

TL;DR; if we have to verify that there's only one addrrspacecast in debug printout, this is OK.
If you figure out the way to test non-debug output, I'd suggest switching to autogenerated checks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've done my best to update the test using @arsenm's suggestion in #122912 (comment). I've also removed the -NOT checks and tried to make the checks as generic as possible to avoid unnecessary dependencies on debug printing format.

However, I'm still not yet able to prevent some other sort of CSE in SDAG from making the effects of this change difficult to observe even directly after SDAG via -stop-after=finalize-isel.

Does the updated version look good?

; CHECK-LABEL: ===== Instruction selection begins
;
%a1 = addrspacecast ptr %p to ptr addrspace(5)
call void @llvm.stackrestore(ptr %p)
store ptr %p, ptr addrspace(5) %a1
ret void
}
Loading