Skip to content

Commit 40986fe

Browse files
authored
Revert "DiagnosticInfo: Clean up usage of DiagnosticInfoInlineAsm" (#119575)
Reverts #119485 Breaks builders, details in #119485
1 parent 323bedd commit 40986fe

15 files changed

+82
-167
lines changed

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -555,18 +555,13 @@ class MachineInstr
555555
/// will be dropped.
556556
void dropDebugNumber() { DebugInstrNum = 0; }
557557

558-
/// For inline asm, get the !srcloc metadata node if we have it, and decode
559-
/// the loc cookie from it.
560-
const MDNode *getLocCookieMD() const;
561-
562-
/// Emit an error referring to the source location of this instruction. This
563-
/// should only be used for inline assembly that is somehow impossible to
564-
/// compile. Other errors should have been handled much earlier.
565-
void emitInlineAsmError(const Twine &ErrMsg) const;
566-
567-
// Emit an error in the LLVMContext referring to the source location of this
568-
// instruction, if available.
569-
void emitGenericError(const Twine &ErrMsg) const;
558+
/// Emit an error referring to the source location of this instruction.
559+
/// This should only be used for inline assembly that is somehow
560+
/// impossible to compile. Other errors should have been handled much
561+
/// earlier.
562+
///
563+
/// If this method returns, the caller should try to recover from the error.
564+
void emitError(StringRef Msg) const;
570565

571566
/// Returns the target instruction descriptor of this MachineInstr.
572567
const MCInstrDesc &getDesc() const { return *MCID; }

llvm/include/llvm/IR/DiagnosticInfo.h

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ enum DiagnosticSeverity : char {
5858
/// Defines the different supported kind of a diagnostic.
5959
/// This enum should be extended with a new ID for each added concrete subclass.
6060
enum DiagnosticKind {
61-
DK_Generic,
62-
DK_GenericWithLoc,
6361
DK_InlineAsm,
6462
DK_ResourceLimit,
6563
DK_StackSize,
@@ -136,33 +134,6 @@ class DiagnosticInfo {
136134

137135
using DiagnosticHandlerFunction = std::function<void(const DiagnosticInfo &)>;
138136

139-
class DiagnosticInfoGeneric : public DiagnosticInfo {
140-
const Twine &MsgStr;
141-
const Instruction *Inst = nullptr;
142-
143-
public:
144-
/// \p MsgStr is the message to be reported to the frontend.
145-
/// This class does not copy \p MsgStr, therefore the reference must be valid
146-
/// for the whole life time of the Diagnostic.
147-
DiagnosticInfoGeneric(const Twine &MsgStr,
148-
DiagnosticSeverity Severity = DS_Error)
149-
: DiagnosticInfo(DK_Generic, Severity), MsgStr(MsgStr) {}
150-
151-
DiagnosticInfoGeneric(const Instruction *I, const Twine &ErrMsg,
152-
DiagnosticSeverity Severity = DS_Warning)
153-
: DiagnosticInfo(DK_Generic, Severity), MsgStr(ErrMsg), Inst(I) {}
154-
155-
const Twine &getMsgStr() const { return MsgStr; }
156-
const Instruction *getInstruction() const { return Inst; }
157-
158-
/// \see DiagnosticInfo::print.
159-
void print(DiagnosticPrinter &DP) const override;
160-
161-
static bool classof(const DiagnosticInfo *DI) {
162-
return DI->getKind() == DK_Generic;
163-
}
164-
};
165-
166137
/// Diagnostic information for inline asm reporting.
167138
/// This is basically a message and an optional location.
168139
class DiagnosticInfoInlineAsm : public DiagnosticInfo {
@@ -175,12 +146,21 @@ class DiagnosticInfoInlineAsm : public DiagnosticInfo {
175146
const Instruction *Instr = nullptr;
176147

177148
public:
149+
/// \p MsgStr is the message to be reported to the frontend.
150+
/// This class does not copy \p MsgStr, therefore the reference must be valid
151+
/// for the whole life time of the Diagnostic.
152+
DiagnosticInfoInlineAsm(const Twine &MsgStr,
153+
DiagnosticSeverity Severity = DS_Error)
154+
: DiagnosticInfo(DK_InlineAsm, Severity), MsgStr(MsgStr) {}
155+
178156
/// \p LocCookie if non-zero gives the line number for this report.
179157
/// \p MsgStr gives the message.
180158
/// This class does not copy \p MsgStr, therefore the reference must be valid
181159
/// for the whole life time of the Diagnostic.
182160
DiagnosticInfoInlineAsm(uint64_t LocCookie, const Twine &MsgStr,
183-
DiagnosticSeverity Severity = DS_Error);
161+
DiagnosticSeverity Severity = DS_Error)
162+
: DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
163+
MsgStr(MsgStr) {}
184164

185165
/// \p Instr gives the original instruction that triggered the diagnostic.
186166
/// \p MsgStr gives the message.
@@ -374,31 +354,6 @@ class DiagnosticInfoWithLocationBase : public DiagnosticInfo {
374354
DiagnosticLocation Loc;
375355
};
376356

377-
class DiagnosticInfoGenericWithLoc : public DiagnosticInfoWithLocationBase {
378-
private:
379-
/// Message to be reported.
380-
const Twine &MsgStr;
381-
382-
public:
383-
/// \p MsgStr is the message to be reported to the frontend.
384-
/// This class does not copy \p MsgStr, therefore the reference must be valid
385-
/// for the whole life time of the Diagnostic.
386-
DiagnosticInfoGenericWithLoc(const Twine &MsgStr, const Function &Fn,
387-
const DiagnosticLocation &Loc,
388-
DiagnosticSeverity Severity = DS_Error)
389-
: DiagnosticInfoWithLocationBase(DK_GenericWithLoc, Severity, Fn, Loc),
390-
MsgStr(MsgStr) {}
391-
392-
const Twine &getMsgStr() const { return MsgStr; }
393-
394-
/// \see DiagnosticInfo::print.
395-
void print(DiagnosticPrinter &DP) const override;
396-
397-
static bool classof(const DiagnosticInfo *DI) {
398-
return DI->getKind() == DK_GenericWithLoc;
399-
}
400-
};
401-
402357
/// Diagnostic information for stack size etc. reporting.
403358
/// This is basically a function and a size.
404359
class DiagnosticInfoResourceLimit : public DiagnosticInfoWithLocationBase {

llvm/include/llvm/IR/LLVMContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ class LLVMContext {
305305
/// be prepared to drop the erroneous construct on the floor and "not crash".
306306
/// The generated code need not be correct. The error message will be
307307
/// implicitly prefixed with "error: " and should not end with a ".".
308+
void emitError(uint64_t LocCookie, const Twine &ErrorStr);
308309
void emitError(const Instruction *I, const Twine &ErrorStr);
309310
void emitError(const Twine &ErrorStr);
310311

llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,10 @@ static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
312312
}
313313
}
314314
if (Error) {
315-
const Function &Fn = MI->getMF()->getFunction();
316-
DiagnosticInfoInlineAsm DI(LocCookie,
317-
"invalid operand in inline asm: '" +
318-
Twine(AsmStr) + "'");
319-
Fn.getContext().diagnose(DI);
315+
std::string msg;
316+
raw_string_ostream Msg(msg);
317+
Msg << "invalid operand in inline asm: '" << AsmStr << "'";
318+
MMI->getModule()->getContext().emitError(LocCookie, msg);
320319
}
321320
}
322321
break;
@@ -348,11 +347,20 @@ void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
348347
// enabled, so we use emitRawComment.
349348
OutStreamer->emitRawComment(MAI->getInlineAsmStart());
350349

351-
const MDNode *LocMD = MI->getLocCookieMD();
352-
uint64_t LocCookie =
353-
LocMD
354-
? mdconst::extract<ConstantInt>(LocMD->getOperand(0))->getZExtValue()
355-
: 0;
350+
// Get the !srcloc metadata node if we have it, and decode the loc cookie from
351+
// it.
352+
uint64_t LocCookie = 0;
353+
const MDNode *LocMD = nullptr;
354+
for (const MachineOperand &MO : llvm::reverse(MI->operands())) {
355+
if (MO.isMetadata() && (LocMD = MO.getMetadata()) &&
356+
LocMD->getNumOperands() != 0) {
357+
if (const ConstantInt *CI =
358+
mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
359+
LocCookie = CI->getZExtValue();
360+
break;
361+
}
362+
}
363+
}
356364

357365
// Emit the inline asm to a temporary string so we can emit it through
358366
// EmitInlineAsm.
@@ -389,23 +397,20 @@ void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
389397
Msg += LS;
390398
Msg += TRI->getRegAsmName(RR);
391399
}
392-
393-
const Function &Fn = MF->getFunction();
394400
const char *Note =
395401
"Reserved registers on the clobber list may not be "
396402
"preserved across the asm statement, and clobbering them may "
397403
"lead to undefined behaviour.";
398-
LLVMContext &Ctx = Fn.getContext();
399-
Ctx.diagnose(DiagnosticInfoInlineAsm(LocCookie, Msg,
400-
DiagnosticSeverity::DS_Warning));
401-
Ctx.diagnose(
404+
MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
405+
LocCookie, Msg, DiagnosticSeverity::DS_Warning));
406+
MMI->getModule()->getContext().diagnose(
402407
DiagnosticInfoInlineAsm(LocCookie, Note, DiagnosticSeverity::DS_Note));
403408

404409
for (const Register RR : RestrRegs) {
405410
if (std::optional<std::string> reason =
406411
TRI->explainReservedReg(*MF, RR)) {
407-
Ctx.diagnose(DiagnosticInfoInlineAsm(LocCookie, *reason,
408-
DiagnosticSeverity::DS_Note));
412+
MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
413+
LocCookie, *reason, DiagnosticSeverity::DS_Note));
409414
}
410415
}
411416
}

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,36 +2219,26 @@ MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
22192219
return hash_combine_range(HashComponents.begin(), HashComponents.end());
22202220
}
22212221

2222-
const MDNode *MachineInstr::getLocCookieMD() const {
2222+
void MachineInstr::emitError(StringRef Msg) const {
22232223
// Find the source location cookie.
2224+
uint64_t LocCookie = 0;
22242225
const MDNode *LocMD = nullptr;
22252226
for (unsigned i = getNumOperands(); i != 0; --i) {
22262227
if (getOperand(i-1).isMetadata() &&
22272228
(LocMD = getOperand(i-1).getMetadata()) &&
22282229
LocMD->getNumOperands() != 0) {
2229-
if (mdconst::hasa<ConstantInt>(LocMD->getOperand(0)))
2230-
return LocMD;
2230+
if (const ConstantInt *CI =
2231+
mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
2232+
LocCookie = CI->getZExtValue();
2233+
break;
2234+
}
22312235
}
22322236
}
22332237

2234-
return nullptr;
2235-
}
2236-
2237-
void MachineInstr::emitInlineAsmError(const Twine &Msg) const {
2238-
assert(isInlineAsm());
2239-
const MDNode *LocMD = getLocCookieMD();
2240-
uint64_t LocCookie =
2241-
LocMD
2242-
? mdconst::extract<ConstantInt>(LocMD->getOperand(0))->getZExtValue()
2243-
: 0;
2244-
LLVMContext &Ctx = getMF()->getFunction().getContext();
2245-
Ctx.diagnose(DiagnosticInfoInlineAsm(LocCookie, Msg));
2246-
}
2247-
2248-
void MachineInstr::emitGenericError(const Twine &Msg) const {
2249-
const Function &Fn = getMF()->getFunction();
2250-
Fn.getContext().diagnose(
2251-
DiagnosticInfoGenericWithLoc(Msg, Fn, getDebugLoc()));
2238+
if (const MachineBasicBlock *MBB = getParent())
2239+
if (const MachineFunction *MF = MBB->getParent())
2240+
return MF->getFunction().getContext().emitError(LocCookie, Msg);
2241+
report_fatal_error(Msg);
22522242
}
22532243

22542244
MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,

llvm/lib/CodeGen/RegAllocBase.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ void RegAllocBase::allocatePhysRegs() {
127127
if (AllocOrder.empty())
128128
report_fatal_error("no registers from class available to allocate");
129129
else if (MI && MI->isInlineAsm()) {
130-
MI->emitInlineAsmError(
131-
"inline assembly requires more registers than available");
130+
MI->emitError("inline assembly requires more registers than available");
132131
} else if (MI) {
133132
LLVMContext &Context =
134133
MI->getParent()->getParent()->getFunction().getContext();

llvm/lib/CodeGen/RegAllocFast.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -964,10 +964,9 @@ void RegAllocFastImpl::allocVirtReg(MachineInstr &MI, LiveReg &LR,
964964
// Nothing we can do: Report an error and keep going with an invalid
965965
// allocation.
966966
if (MI.isInlineAsm())
967-
MI.emitInlineAsmError(
968-
"inline assembly requires more registers than available");
967+
MI.emitError("inline assembly requires more registers than available");
969968
else
970-
MI.emitInlineAsmError("ran out of registers during register allocation");
969+
MI.emitError("ran out of registers during register allocation");
971970

972971
LR.Error = true;
973972
LR.PhysReg = 0;

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,13 @@ getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts,
319319
static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V,
320320
const Twine &ErrMsg) {
321321
const Instruction *I = dyn_cast_or_null<Instruction>(V);
322-
if (!I)
322+
if (!V)
323323
return Ctx.emitError(ErrMsg);
324324

325+
const char *AsmError = ", possible invalid constraint for vector type";
325326
if (const CallInst *CI = dyn_cast<CallInst>(I))
326-
if (CI->isInlineAsm()) {
327-
return Ctx.diagnose(DiagnosticInfoInlineAsm(
328-
*CI, ErrMsg + ", possible invalid constraint for vector type"));
329-
}
327+
if (CI->isInlineAsm())
328+
return Ctx.emitError(I, ErrMsg + AsmError);
330329

331330
return Ctx.emitError(I, ErrMsg);
332331
}
@@ -10504,7 +10503,7 @@ void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
1050410503
void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
1050510504
const Twine &Message) {
1050610505
LLVMContext &Ctx = *DAG.getContext();
10507-
Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10506+
Ctx.emitError(&Call, Message);
1050810507

1050910508
// Make sure we leave the DAG in a valid state
1051010509
const TargetLowering &TLI = DAG.getTargetLoweringInfo();

llvm/lib/CodeGen/XRayInstrumentation.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "llvm/CodeGen/TargetInstrInfo.h"
2525
#include "llvm/CodeGen/TargetSubtargetInfo.h"
2626
#include "llvm/IR/Attributes.h"
27-
#include "llvm/IR/DiagnosticInfo.h"
2827
#include "llvm/IR/Function.h"
2928
#include "llvm/InitializePasses.h"
3029
#include "llvm/Pass.h"
@@ -212,12 +211,8 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
212211
auto &FirstMI = *FirstMBB.begin();
213212

214213
if (!MF.getSubtarget().isXRaySupported()) {
215-
216-
const Function &Fn = FirstMBB.getParent()->getFunction();
217-
Fn.getContext().diagnose(DiagnosticInfoUnsupported(
218-
Fn, "An attempt to perform XRay instrumentation for an"
219-
" unsupported target."));
220-
214+
FirstMI.emitError("An attempt to perform XRay instrumentation for an"
215+
" unsupported target.");
221216
return false;
222217
}
223218

llvm/lib/IR/DiagnosticInfo.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,6 @@ int llvm::getNextAvailablePluginDiagnosticKind() {
4848

4949
const char *OptimizationRemarkAnalysis::AlwaysPrint = "";
5050

51-
void DiagnosticInfoGeneric::print(DiagnosticPrinter &DP) const {
52-
DP << getMsgStr();
53-
}
54-
55-
void DiagnosticInfoGenericWithLoc::print(DiagnosticPrinter &DP) const {
56-
DP << getLocationStr() << ": " << getMsgStr();
57-
}
58-
59-
DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(uint64_t LocCookie,
60-
const Twine &MsgStr,
61-
DiagnosticSeverity Severity)
62-
: DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
63-
MsgStr(MsgStr) {}
64-
6551
DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
6652
const Twine &MsgStr,
6753
DiagnosticSeverity Severity)

llvm/lib/IR/LLVMContext.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,12 @@ void LLVMContext::yield() {
219219
}
220220

221221
void LLVMContext::emitError(const Twine &ErrorStr) {
222-
diagnose(DiagnosticInfoGeneric(ErrorStr));
222+
diagnose(DiagnosticInfoInlineAsm(ErrorStr));
223223
}
224224

225225
void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
226-
assert(I && "Invalid instruction");
227-
diagnose(DiagnosticInfoGeneric(I, ErrorStr));
226+
assert (I && "Invalid instruction");
227+
diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
228228
}
229229

230230
static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
@@ -283,6 +283,10 @@ void LLVMContext::diagnose(const DiagnosticInfo &DI) {
283283
exit(1);
284284
}
285285

286+
void LLVMContext::emitError(uint64_t LocCookie, const Twine &ErrorStr) {
287+
diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
288+
}
289+
286290
//===----------------------------------------------------------------------===//
287291
// Metadata Kind Uniquing
288292
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)