Skip to content

Commit 6149fa1

Browse files
committed
Address review comments
- Add IRBuilder::resetModeToStrictFP instead of adding extra argument to setIsFPConstrained. - Change wording in the Release Notes. - Modify printing MemoryEffects of FP operation. - Fix IRBuilder::CreateCall. - Clean up unit test.
1 parent a01d4ad commit 6149fa1

File tree

17 files changed

+154
-123
lines changed

17 files changed

+154
-123
lines changed

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
10891089
if ((FD && (FD->UsesFPIntrin() || FD->hasAttr<StrictFPAttr>())) ||
10901090
(!FD && (FPExceptionBehavior != llvm::fp::ebIgnore ||
10911091
RM != llvm::RoundingMode::NearestTiesToEven))) {
1092-
Builder.setIsFPConstrained(true, false);
1092+
Builder.setIsFPConstrained(true);
10931093
Fn->addFnAttr(llvm::Attribute::StrictFP);
10941094
}
10951095

llvm/docs/ReleaseNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Changes to the LLVM IR
6868
pointers) argument.
6969
* A `load atomic` may now be used with vector types on x86.
7070
* Floating-point operand bundles have been added.
71+
* Calls to floating-point intrinsics can have operand bundles "fp.round" and
72+
"fp.except", which specify effective rounding mode and exception behavior.
7173

7274
Changes to LLVM infrastructure
7375
------------------------------

llvm/include/llvm/IR/FPEnv.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ LLVM_ABI std::optional<StringRef>
8181
std::optional<StringRef>
8282
convertExceptionBehaviorToBundle(fp::ExceptionBehavior);
8383

84+
inline raw_ostream &operator<<(raw_ostream &OS, fp::ExceptionBehavior EB) {
85+
OS << convertExceptionBehaviorToBundle(EB).value_or("invalid");
86+
return OS;
87+
}
88+
8489
/// Returns true if the exception handling behavior and rounding mode
8590
/// match what is used in the default floating point environment.
8691
inline bool isDefaultFPEnvironment(fp::ExceptionBehavior EB, RoundingMode RM) {

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,17 +348,19 @@ class IRBuilderBase {
348348
/// enabled the CreateF<op>() calls instead create constrained
349349
/// floating point intrinsic calls. Fast math flags are unaffected
350350
/// by this setting.
351-
void setIsFPConstrained(bool IsCon, bool AndReset = true) {
352-
if (AndReset) {
353-
if (IsCon) {
354-
setDefaultConstrainedRounding(RoundingMode::Dynamic);
355-
setDefaultConstrainedExcept(fp::ebStrict);
356-
} else {
357-
setDefaultConstrainedRounding(RoundingMode::NearestTiesToEven);
358-
setDefaultConstrainedExcept(fp::ebIgnore);
359-
}
351+
void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
352+
353+
/// Enable/Disable use of constrained floating point math and reset FP options
354+
/// according to the selected mode.
355+
void resetModeToStrictFP(bool IsCon) {
356+
if (IsCon) {
357+
setDefaultConstrainedRounding(RoundingMode::Dynamic);
358+
setDefaultConstrainedExcept(fp::ebStrict);
359+
} else {
360+
setDefaultConstrainedRounding(RoundingMode::NearestTiesToEven);
361+
setDefaultConstrainedExcept(fp::ebIgnore);
360362
}
361-
IsFPConstrained = IsCon;
363+
setIsFPConstrained(IsCon);
362364
}
363365

364366
/// Query for the use of constrained floating point math

llvm/lib/CodeGen/AtomicExpandPass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ struct ReplacementIRBuilder
159159
SetInsertPoint(I);
160160
this->CollectMetadataToCopy(I, {LLVMContext::MD_pcsections});
161161
if (BB->getParent()->getAttributes().hasFnAttr(Attribute::StrictFP))
162-
this->setIsFPConstrained(true);
162+
this->resetModeToStrictFP(true);
163163

164164
MMRAMD = I->getMetadata(LLVMContext::MD_mmra);
165165
}
@@ -1716,7 +1716,7 @@ bool AtomicExpandImpl::tryExpandAtomicCmpXchg(AtomicCmpXchgInst *CI) {
17161716
bool llvm::expandAtomicRMWToCmpXchg(AtomicRMWInst *AI,
17171717
CreateCmpXchgInstFun CreateCmpXchg) {
17181718
ReplacementIRBuilder Builder(AI, AI->getDataLayout());
1719-
Builder.setIsFPConstrained(
1719+
Builder.resetModeToStrictFP(
17201720
AI->getFunction()->hasFnAttribute(Attribute::StrictFP));
17211721

17221722
// FIXME: If FP exceptions are observable, we should force them off for the

llvm/lib/CodeGen/HardwareLoops.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ Value *HardwareLoop::InitLoopCount() {
503503
Value* HardwareLoop::InsertIterationSetup(Value *LoopCountInit) {
504504
IRBuilder<> Builder(BeginBB->getTerminator());
505505
if (BeginBB->getParent()->getAttributes().hasFnAttr(Attribute::StrictFP))
506-
Builder.setIsFPConstrained(true);
506+
Builder.resetModeToStrictFP(true);
507507
Type *Ty = LoopCountInit->getType();
508508
bool UsePhi = UsePHICounter || Opts.ForcePhi;
509509
Intrinsic::ID ID = UseLoopGuard
@@ -537,7 +537,7 @@ void HardwareLoop::InsertLoopDec() {
537537
IRBuilder<> CondBuilder(ExitBranch);
538538
if (ExitBranch->getParent()->getParent()->getAttributes().hasFnAttr(
539539
Attribute::StrictFP))
540-
CondBuilder.setIsFPConstrained(true);
540+
CondBuilder.resetModeToStrictFP(true);
541541

542542
Value *Ops[] = { LoopDecrement };
543543
Value *NewCond = CondBuilder.CreateIntrinsic(Intrinsic::loop_decrement,
@@ -560,7 +560,7 @@ Instruction* HardwareLoop::InsertLoopRegDec(Value *EltsRem) {
560560
IRBuilder<> CondBuilder(ExitBranch);
561561
if (ExitBranch->getParent()->getParent()->getAttributes().hasFnAttr(
562562
Attribute::StrictFP))
563-
CondBuilder.setIsFPConstrained(true);
563+
CondBuilder.resetModeToStrictFP(true);
564564

565565
Value *Ops[] = { EltsRem, LoopDecrement };
566566
Value *Call = CondBuilder.CreateIntrinsic(Intrinsic::loop_decrement_reg,

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8360,33 +8360,6 @@ void SelectionDAGBuilder::pushFPOpOutChain(SDValue Result,
83608360
}
83618361
}
83628362

8363-
void SelectionDAGBuilder::pushOutChain(SDValue Result,
8364-
fp::ExceptionBehavior EB) {
8365-
assert(Result.getNode()->getNumValues() == 2);
8366-
8367-
// Push node to the appropriate list so that future instructions can be
8368-
// chained up correctly.
8369-
SDValue OutChain = Result.getValue(1);
8370-
switch (EB) {
8371-
case fp::ExceptionBehavior::ebIgnore:
8372-
// The only reason why ebIgnore nodes still need to be chained is that
8373-
// they might depend on the current rounding mode, and therefore must
8374-
// not be moved across instruction that may change that mode.
8375-
[[fallthrough]];
8376-
case fp::ExceptionBehavior::ebMayTrap:
8377-
// These must not be moved across calls or instructions that may change
8378-
// floating-point exception masks.
8379-
PendingConstrainedFP.push_back(OutChain);
8380-
break;
8381-
case fp::ExceptionBehavior::ebStrict:
8382-
// These must not be moved across calls or instructions that may change
8383-
// floating-point exception masks or read floating-point exception flags.
8384-
// In addition, they cannot be optimized out even if unused.
8385-
PendingConstrainedFPStrict.push_back(OutChain);
8386-
break;
8387-
}
8388-
}
8389-
83908363
void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
83918364
const ConstrainedFPIntrinsic &FPI) {
83928365
SDLoc sdl = getCurSDLoc();
@@ -9496,7 +9469,7 @@ bool SelectionDAGBuilder::visitFPOperation(const CallInst &I, unsigned Opcode) {
94969469
SDLoc sdl = getCurSDLoc();
94979470
SDValue Result = DAG.getNode(Opcode, sdl, NodeVT, Operands, Flags);
94989471
if (HasChain)
9499-
pushOutChain(Result, EB);
9472+
pushFPOpOutChain(Result, EB);
95009473

95019474
SDValue FPResult = Result.getValue(0);
95029475
setValue(&I, FPResult);

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,6 @@ class SelectionDAGBuilder {
379379
/// Evict any dangling debug information, attempting to salvage it first.
380380
void resolveOrClearDbgInfo();
381381

382-
void pushOutChain(SDValue Result, fp::ExceptionBehavior EB);
383-
384382
SDValue getValue(const Value *V);
385383

386384
SDValue getNonRegisterValue(const Value *V);

llvm/lib/IR/AsmWriter.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ static cl::opt<bool> PreserveAssemblyUseListOrder(
106106
"preserve-ll-uselistorder", cl::Hidden, cl::init(false),
107107
cl::desc("Preserve use-list order when writing LLVM assembly."));
108108

109+
static cl::opt<bool> PrintFPMemoryEffects(
110+
"print-fp-memory-effects", cl::Hidden,
111+
cl::desc("Pretty print floating-point memory effects when dumping"));
112+
109113
// Make virtual table appear in this compilation unit.
110114
AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
111115

@@ -4321,6 +4325,25 @@ void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
43214325
Out << ")";
43224326
}
43234327

4328+
static void printFPMemoryEffects(raw_ostream &Out, MemoryEffects ME) {
4329+
ModRefInfo MR = ME.getModRef(IRMemLocation::InaccessibleMem);
4330+
Out << " ; fpe=[";
4331+
switch (MR) {
4332+
case ModRefInfo::NoModRef:
4333+
break;
4334+
case ModRefInfo::Ref:
4335+
Out << "r";
4336+
break;
4337+
case ModRefInfo::Mod:
4338+
Out << "w";
4339+
break;
4340+
case ModRefInfo::ModRef:
4341+
Out << "rw";
4342+
break;
4343+
}
4344+
Out << "]";
4345+
}
4346+
43244347
/// printInfoComment - Print a little comment after the instruction indicating
43254348
/// which slot it occupies.
43264349
void AssemblyWriter::printInfoComment(const Value &V) {
@@ -4351,30 +4374,14 @@ void AssemblyWriter::printInfoComment(const Value &V) {
43514374
if (PrintInstAddrs)
43524375
Out << " ; " << &V;
43534376

4354-
if (auto *CI = dyn_cast<CallInst>(&V))
4355-
if (Intrinsic::ID IID = CI->getIntrinsicID())
4356-
if (IntrinsicInst::isFloatingPointOperation(IID))
4357-
if (const BasicBlock *BB = CI->getParent())
4358-
if (const Function *F = BB->getParent())
4359-
if (F->hasFnAttribute(Attribute::StrictFP)) {
4360-
MemoryEffects ME = CI->getMemoryEffects();
4361-
ModRefInfo MR = ME.getModRef(IRMemLocation::InaccessibleMem);
4362-
Out << " ; fpe=[";
4363-
switch (MR) {
4364-
case ModRefInfo::NoModRef:
4365-
break;
4366-
case ModRefInfo::Ref:
4367-
Out << "r";
4368-
break;
4369-
case ModRefInfo::Mod:
4370-
Out << "w";
4371-
break;
4372-
case ModRefInfo::ModRef:
4373-
Out << "rw";
4374-
break;
4375-
}
4376-
Out << "]";
4377-
}
4377+
if (PrintFPMemoryEffects) {
4378+
if (auto *CI = dyn_cast<CallInst>(&V))
4379+
if (Intrinsic::ID IID = CI->getIntrinsicID())
4380+
if (const Function *F = CI->getFunction())
4381+
if (IntrinsicInst::isFloatingPointOperation(IID) &&
4382+
F->hasFnAttribute(Attribute::StrictFP))
4383+
printFPMemoryEffects(Out, CI->getMemoryEffects());
4384+
}
43784385
}
43794386

43804387
static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,

llvm/lib/IR/FPEnv.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,20 @@ std::optional<StringRef> convertRoundingModeToBundle(RoundingMode UseRounding) {
7676
std::optional<StringRef> RoundingStr;
7777
switch (UseRounding) {
7878
case RoundingMode::Dynamic:
79-
RoundingStr = "dynamic";
80-
break;
79+
return "dynamic";
8180
case RoundingMode::NearestTiesToEven:
82-
RoundingStr = "tonearest";
83-
break;
81+
return "tonearest";
8482
case RoundingMode::NearestTiesToAway:
85-
RoundingStr = "tonearestaway";
86-
break;
83+
return "tonearestaway";
8784
case RoundingMode::TowardNegative:
88-
RoundingStr = "downward";
89-
break;
85+
return "downward";
9086
case RoundingMode::TowardPositive:
91-
RoundingStr = "upward";
92-
break;
87+
return "upward";
9388
case RoundingMode::TowardZero:
94-
RoundingStr = "towardzero";
95-
break;
89+
return "towardzero";
9690
default:
97-
break;
91+
return std::nullopt;
9892
}
99-
return RoundingStr;
10093
}
10194

10295
std::optional<fp::ExceptionBehavior>
@@ -139,16 +132,14 @@ convertExceptionBehaviorToBundle(fp::ExceptionBehavior UseExcept) {
139132
std::optional<StringRef> ExceptStr;
140133
switch (UseExcept) {
141134
case fp::ebStrict:
142-
ExceptStr = "strict";
143-
break;
135+
return "strict";
144136
case fp::ebIgnore:
145-
ExceptStr = "ignore";
146-
break;
137+
return "ignore";
147138
case fp::ebMayTrap:
148-
ExceptStr = "maytrap";
149-
break;
139+
return "maytrap";
140+
default:
141+
return std::nullopt;
150142
}
151-
return ExceptStr;
152143
}
153144

154145
Intrinsic::ID getConstrainedIntrinsicID(const Instruction &Instr) {

0 commit comments

Comments
 (0)