Skip to content

[X86][APX] Do optimizeMemoryInst for v1X masked load/store #151331

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

Merged
merged 3 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions llvm/lib/CodeGen/CodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2769,6 +2769,29 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
return optimizeGatherScatterInst(II, II->getArgOperand(0));
case Intrinsic::masked_scatter:
return optimizeGatherScatterInst(II, II->getArgOperand(1));
case Intrinsic::masked_load:
// Treat v1X masked load as load X type.
if (auto *VT = dyn_cast<FixedVectorType>(II->getType())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a test for load too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have one in llvm/test/CodeGen/X86/isel-sink.ll: https://godbolt.org/z/5zhjr4ses

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I misunderstood your point. Added in f51c6bd

if (VT->getNumElements() == 1) {
Value *PtrVal = II->getArgOperand(0);
unsigned AS = PtrVal->getType()->getPointerAddressSpace();
if (optimizeMemoryInst(II, PtrVal, VT->getElementType(), AS))
return true;
}
}
return false;
case Intrinsic::masked_store:
// Treat v1X masked store as store X type.
if (auto *VT =
dyn_cast<FixedVectorType>(II->getArgOperand(0)->getType())) {
if (VT->getNumElements() == 1) {
Value *PtrVal = II->getArgOperand(1);
unsigned AS = PtrVal->getType()->getPointerAddressSpace();
if (optimizeMemoryInst(II, PtrVal, VT->getElementType(), AS))
return true;
}
}
return false;
}

SmallVector<Value *, 2> PtrOps;
Expand Down
21 changes: 21 additions & 0 deletions llvm/test/CodeGen/X86/apx/cf.ll
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,24 @@ entry:
call void @llvm.masked.store.v1i64.p0(<1 x i64> %3, ptr %p, i32 4, <1 x i1> %0)
ret void
}

define void @sink_gep(ptr %p, i1 %cond) {
; CHECK-LABEL: sink_gep:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: xorl %eax, %eax
; CHECK-NEXT: testb $1, %sil
; CHECK-NEXT: cfcmovnel %eax, 112(%rdi)
; CHECK-NEXT: cfcmovnel 112(%rdi), %eax
; CHECK-NEXT: movl %eax, (%rdi)
; CHECK-NEXT: retq
entry:
%0 = getelementptr i8, ptr %p, i64 112
br label %next

next:
%1 = bitcast i1 %cond to <1 x i1>
call void @llvm.masked.store.v1i32.p0(<1 x i32> zeroinitializer, ptr %0, i32 1, <1 x i1> %1)
%2 = call <1 x i32> @llvm.masked.load.v1i32.p0(ptr %0, i32 1, <1 x i1> %1, <1 x i32> zeroinitializer)
store <1 x i32> %2, ptr %p, align 4
ret void
}