Skip to content

Commit 4a17c21

Browse files
committed
Address code review feedback.
1 parent cdc2571 commit 4a17c21

File tree

4 files changed

+29
-37
lines changed

4 files changed

+29
-37
lines changed

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4013,7 +4013,6 @@ bool SPIRVInstructionSelector::selectModf(Register ResVReg,
40134013
// independent elements of SPIRVResType. We can get each independent element
40144014
// from I.getDefs() or I.getOperands().
40154015
if (STI.canUseExtInstSet(SPIRV::InstructionSet::OpenCL_std)) {
4016-
uint32_t Opcode = CL::modf;
40174016
MachineIRBuilder MIRBuilder(I);
40184017
// Get pointer type for alloca variable.
40194018
const SPIRVType *PtrType = GR.getOrCreateSPIRVPointerType(
@@ -4029,28 +4028,7 @@ bool SPIRVInstructionSelector::selectModf(Register ResVReg,
40294028
// new register.
40304029
GR.assignSPIRVTypeToVReg(PtrType, PtrTyReg, MIRBuilder.getMF());
40314030
MachineBasicBlock &EntryBB = I.getMF()->front();
4032-
// At this point it's difficult to find the right position to insert the
4033-
// variable, because most instructions are still MachineInstruction and
4034-
// don't have SPIRV opcodes yet. OpFunction and OpFunctionParameter are
4035-
// already translated, so we will aim to insert the variable just after the
4036-
// last OpFunctionParameter, if any, or just after OpFunction otherwise.
4037-
auto VarPos = EntryBB.begin();
4038-
while (VarPos != EntryBB.end() &&
4039-
VarPos->getOpcode() != SPIRV::OpFunction) {
4040-
++VarPos;
4041-
}
4042-
// Advance VarPos to the next instruction after OpFunction, it will either
4043-
// be an OpFunctionParameter, so that we can start the next loop, or the
4044-
// position to insert the OpVariable instruction.
4045-
++VarPos;
4046-
while (VarPos != EntryBB.end() &&
4047-
VarPos->getOpcode() == SPIRV::OpFunctionParameter) {
4048-
++VarPos;
4049-
}
4050-
// VarPos is now pointing at after the last OpFunctionParameter, if any,
4051-
// or after OpFunction, if no parameters.
4052-
// Create a new MachineInstruction for alloca variable in the
4053-
// entry block.
4031+
MachineBasicBlock::iterator VarPos = getPosForOpVariableWithinBlock(EntryBB);
40544032
auto AllocaMIB =
40554033
BuildMI(EntryBB, VarPos, I.getDebugLoc(), TII.get(SPIRV::OpVariable))
40564034
.addDef(PtrTyReg)
@@ -4068,7 +4046,7 @@ bool SPIRVInstructionSelector::selectModf(Register ResVReg,
40684046
.addDef(ResVReg)
40694047
.addUse(GR.getSPIRVTypeID(ResType))
40704048
.addImm(static_cast<uint32_t>(SPIRV::InstructionSet::OpenCL_std))
4071-
.addImm(Opcode)
4049+
.addImm(CL::modf)
40724050
.setMIFlags(I.getFlags())
40734051
.add(I.getOperand(3)) // Floating point value.
40744052
.addUse(Variable); // Pointer to integral part.

llvm/lib/Target/SPIRV/SPIRVUtils.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,4 +995,26 @@ unsigned getArrayComponentCount(const MachineRegisterInfo *MRI,
995995
return foldImm(ResType->getOperand(2), MRI);
996996
}
997997

998+
MachineBasicBlock::iterator
999+
getPosForOpVariableWithinBlock(MachineBasicBlock &BB) {
1000+
// Find the position to insert the OpVariable instruction.
1001+
// We will insert it after the last OpFunctionParameter, if any, or
1002+
// after OpFunction otherwise.
1003+
MachineBasicBlock::iterator VarPos = BB.begin();
1004+
while (VarPos != BB.end() && VarPos->getOpcode() != SPIRV::OpFunction) {
1005+
++VarPos;
1006+
}
1007+
// Advance VarPos to the next instruction after OpFunction, it will either
1008+
// be an OpFunctionParameter, so that we can start the next loop, or the
1009+
// position to insert the OpVariable instruction.
1010+
++VarPos;
1011+
while (VarPos != BB.end() &&
1012+
VarPos->getOpcode() == SPIRV::OpFunctionParameter) {
1013+
++VarPos;
1014+
}
1015+
// VarPos is now pointing at after the last OpFunctionParameter, if any,
1016+
// or after OpFunction, if no parameters.
1017+
return VarPos;
1018+
}
1019+
9981020
} // namespace llvm

llvm/lib/Target/SPIRV/SPIRVUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ MachineInstr *getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI);
506506
int64_t foldImm(const MachineOperand &MO, const MachineRegisterInfo *MRI);
507507
unsigned getArrayComponentCount(const MachineRegisterInfo *MRI,
508508
const MachineInstr *ResType);
509+
MachineBasicBlock::iterator
510+
getPosForOpVariableWithinBlock(MachineBasicBlock &BB);
509511

510512
} // namespace llvm
511513
#endif // LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H

llvm/test/CodeGen/SPIRV/llvm-intrinsics/fp-intrinsics.ll

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -361,34 +361,24 @@ entry:
361361

362362
define dso_local void @TestModf2(double noundef %d, ptr noundef %frac, ptr noundef %integral) {
363363
entry:
364-
%d.addr = alloca double, align 4
365-
%frac.addr = alloca ptr, align 8
366-
%integral.addr = alloca ptr, align 8
367-
store double %d, ptr %d.addr, align 4
368-
store ptr %frac, ptr %frac.addr, align 8
369-
store ptr %integral, ptr %integral.addr, align 8
370-
%0 = load ptr, ptr %frac.addr, align 8
364+
%0 = load ptr, ptr %frac, align 8
371365
%tobool = icmp ne ptr %0, null
372366
br i1 %tobool, label %lor.lhs.false, label %if.then
373367

374368
lor.lhs.false:
375-
%1 = load ptr, ptr %integral.addr, align 8
369+
%1 = load ptr, ptr %integral, align 8
376370
%tobool1 = icmp ne ptr %1, null
377371
br i1 %tobool1, label %if.end, label %if.then
378372

379373
if.then:
380374
br label %return
381375

382376
if.end:
383-
%2 = load ptr, ptr %frac.addr, align 8
384-
%3 = load double, ptr %2, align 4
385-
%4 = load ptr, ptr %integral.addr, align 8
386-
%5 = load double, ptr %4, align 4
387377
%6 = tail call { double, double } @llvm.modf.f64(double %d)
388378
%7 = extractvalue { double, double } %6, 0
389379
%8 = extractvalue { double, double } %6, 1
390380
store double %7, ptr %frac, align 4
391-
store double %7, ptr %integral, align 4
381+
store double %8, ptr %integral, align 4
392382
br label %return
393383

394384
return:

0 commit comments

Comments
 (0)