Skip to content
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

[RISCV] Allow non-loop invariant steps in RISCVGatherScatterLowering #122244

Merged
merged 4 commits into from
Jan 17, 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: 17 additions & 6 deletions llvm/lib/Target/RISCV/RISCVGatherScatterLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ bool RISCVGatherScatterLowering::matchStridedRecurrence(Value *Index, Loop *L,
assert(Phi->getIncomingValue(IncrementingBlock) == Inc &&
"Expected one operand of phi to be Inc");

// Only proceed if the step is loop invariant.
if (!L->isLoopInvariant(Step))
return false;

// Step should be a splat.
Step = getSplatValue(Step);
if (!Step)
Expand Down Expand Up @@ -298,6 +294,7 @@ bool RISCVGatherScatterLowering::matchStridedRecurrence(Value *Index, Loop *L,
BasePtr->getIncomingBlock(StartBlock)->getTerminator());
Builder.SetCurrentDebugLocation(DebugLoc());

// TODO: Share this switch with matchStridedStart?
switch (BO->getOpcode()) {
default:
llvm_unreachable("Unexpected opcode!");
Expand All @@ -310,18 +307,32 @@ bool RISCVGatherScatterLowering::matchStridedRecurrence(Value *Index, Loop *L,
}
case Instruction::Mul: {
Start = Builder.CreateMul(Start, SplatOp, "start");
Step = Builder.CreateMul(Step, SplatOp, "step");
Copy link
Collaborator

Choose a reason for hiding this comment

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

This block of code now entirely duplicates the end of matchStridedStart, maybe we can extract out a helper and use it in both places? This an idea, not a requirement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. It looks like matchStridedStart doesn't set the IR names though and I don't want to introduce more potential diff, so I've left a todo to revisit this

Stride = Builder.CreateMul(Stride, SplatOp, "stride");
break;
}
case Instruction::Shl: {
Start = Builder.CreateShl(Start, SplatOp, "start");
Step = Builder.CreateShl(Step, SplatOp, "step");
Stride = Builder.CreateShl(Stride, SplatOp, "stride");
break;
}
}

// If the Step was defined inside the loop, adjust it before its definition
// instead of in the preheader.
if (auto *StepI = dyn_cast<Instruction>(Step); StepI && L->contains(StepI))
Builder.SetInsertPoint(*StepI->getInsertionPointAfterDef());

switch (BO->getOpcode()) {
default:
break;
case Instruction::Mul:
Step = Builder.CreateMul(Step, SplatOp, "step");
break;
case Instruction::Shl:
Step = Builder.CreateShl(Step, SplatOp, "step");
break;
}

Inc->setOperand(StepIndex, Step);
BasePtr->setIncomingValue(StartBlock, Start);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ for.cond.cleanup: ; preds = %vector.body
define void @gather_unknown_pow2(ptr noalias nocapture %A, ptr noalias nocapture readonly %B, i64 %shift) {
; CHECK-LABEL: @gather_unknown_pow2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[STEP:%.*]] = shl i64 8, [[SHIFT:%.*]]
; CHECK-NEXT: [[STRIDE:%.*]] = shl i64 1, [[SHIFT]]
; CHECK-NEXT: [[STRIDE:%.*]] = shl i64 1, [[SHIFT:%.*]]
; CHECK-NEXT: [[STEP:%.*]] = shl i64 8, [[SHIFT]]
; CHECK-NEXT: [[TMP0:%.*]] = mul i64 [[STRIDE]], 4
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
; CHECK: vector.body:
Expand Down
Loading
Loading