Skip to content

Revert "DiagnosticInfo: Clean up usage of DiagnosticInfoInlineAsm" #119575

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
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
19 changes: 7 additions & 12 deletions llvm/include/llvm/CodeGen/MachineInstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,18 +555,13 @@ class MachineInstr
/// will be dropped.
void dropDebugNumber() { DebugInstrNum = 0; }

/// For inline asm, get the !srcloc metadata node if we have it, and decode
/// the loc cookie from it.
const MDNode *getLocCookieMD() const;

/// Emit an error referring to the source location of this instruction. This
/// should only be used for inline assembly that is somehow impossible to
/// compile. Other errors should have been handled much earlier.
void emitInlineAsmError(const Twine &ErrMsg) const;

// Emit an error in the LLVMContext referring to the source location of this
// instruction, if available.
void emitGenericError(const Twine &ErrMsg) const;
/// Emit an error referring to the source location of this instruction.
/// This should only be used for inline assembly that is somehow
/// impossible to compile. Other errors should have been handled much
/// earlier.
///
/// If this method returns, the caller should try to recover from the error.
void emitError(StringRef Msg) const;

/// Returns the target instruction descriptor of this MachineInstr.
const MCInstrDesc &getDesc() const { return *MCID; }
Expand Down
65 changes: 10 additions & 55 deletions llvm/include/llvm/IR/DiagnosticInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ enum DiagnosticSeverity : char {
/// Defines the different supported kind of a diagnostic.
/// This enum should be extended with a new ID for each added concrete subclass.
enum DiagnosticKind {
DK_Generic,
DK_GenericWithLoc,
DK_InlineAsm,
DK_ResourceLimit,
DK_StackSize,
Expand Down Expand Up @@ -136,33 +134,6 @@ class DiagnosticInfo {

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

class DiagnosticInfoGeneric : public DiagnosticInfo {
const Twine &MsgStr;
const Instruction *Inst = nullptr;

public:
/// \p MsgStr is the message to be reported to the frontend.
/// This class does not copy \p MsgStr, therefore the reference must be valid
/// for the whole life time of the Diagnostic.
DiagnosticInfoGeneric(const Twine &MsgStr,
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfo(DK_Generic, Severity), MsgStr(MsgStr) {}

DiagnosticInfoGeneric(const Instruction *I, const Twine &ErrMsg,
DiagnosticSeverity Severity = DS_Warning)
: DiagnosticInfo(DK_Generic, Severity), MsgStr(ErrMsg), Inst(I) {}

const Twine &getMsgStr() const { return MsgStr; }
const Instruction *getInstruction() const { return Inst; }

/// \see DiagnosticInfo::print.
void print(DiagnosticPrinter &DP) const override;

static bool classof(const DiagnosticInfo *DI) {
return DI->getKind() == DK_Generic;
}
};

/// Diagnostic information for inline asm reporting.
/// This is basically a message and an optional location.
class DiagnosticInfoInlineAsm : public DiagnosticInfo {
Expand All @@ -175,12 +146,21 @@ class DiagnosticInfoInlineAsm : public DiagnosticInfo {
const Instruction *Instr = nullptr;

public:
/// \p MsgStr is the message to be reported to the frontend.
/// This class does not copy \p MsgStr, therefore the reference must be valid
/// for the whole life time of the Diagnostic.
DiagnosticInfoInlineAsm(const Twine &MsgStr,
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfo(DK_InlineAsm, Severity), MsgStr(MsgStr) {}

/// \p LocCookie if non-zero gives the line number for this report.
/// \p MsgStr gives the message.
/// This class does not copy \p MsgStr, therefore the reference must be valid
/// for the whole life time of the Diagnostic.
DiagnosticInfoInlineAsm(uint64_t LocCookie, const Twine &MsgStr,
DiagnosticSeverity Severity = DS_Error);
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
MsgStr(MsgStr) {}

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

class DiagnosticInfoGenericWithLoc : public DiagnosticInfoWithLocationBase {
private:
/// Message to be reported.
const Twine &MsgStr;

public:
/// \p MsgStr is the message to be reported to the frontend.
/// This class does not copy \p MsgStr, therefore the reference must be valid
/// for the whole life time of the Diagnostic.
DiagnosticInfoGenericWithLoc(const Twine &MsgStr, const Function &Fn,
const DiagnosticLocation &Loc,
DiagnosticSeverity Severity = DS_Error)
: DiagnosticInfoWithLocationBase(DK_GenericWithLoc, Severity, Fn, Loc),
MsgStr(MsgStr) {}

const Twine &getMsgStr() const { return MsgStr; }

/// \see DiagnosticInfo::print.
void print(DiagnosticPrinter &DP) const override;

static bool classof(const DiagnosticInfo *DI) {
return DI->getKind() == DK_GenericWithLoc;
}
};

/// Diagnostic information for stack size etc. reporting.
/// This is basically a function and a size.
class DiagnosticInfoResourceLimit : public DiagnosticInfoWithLocationBase {
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/IR/LLVMContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ class LLVMContext {
/// be prepared to drop the erroneous construct on the floor and "not crash".
/// The generated code need not be correct. The error message will be
/// implicitly prefixed with "error: " and should not end with a ".".
void emitError(uint64_t LocCookie, const Twine &ErrorStr);
void emitError(const Instruction *I, const Twine &ErrorStr);
void emitError(const Twine &ErrorStr);

Expand Down
41 changes: 23 additions & 18 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,10 @@ static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
}
}
if (Error) {
const Function &Fn = MI->getMF()->getFunction();
DiagnosticInfoInlineAsm DI(LocCookie,
"invalid operand in inline asm: '" +
Twine(AsmStr) + "'");
Fn.getContext().diagnose(DI);
std::string msg;
raw_string_ostream Msg(msg);
Msg << "invalid operand in inline asm: '" << AsmStr << "'";
MMI->getModule()->getContext().emitError(LocCookie, msg);
}
}
break;
Expand Down Expand Up @@ -348,11 +347,20 @@ void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
// enabled, so we use emitRawComment.
OutStreamer->emitRawComment(MAI->getInlineAsmStart());

const MDNode *LocMD = MI->getLocCookieMD();
uint64_t LocCookie =
LocMD
? mdconst::extract<ConstantInt>(LocMD->getOperand(0))->getZExtValue()
: 0;
// Get the !srcloc metadata node if we have it, and decode the loc cookie from
// it.
uint64_t LocCookie = 0;
const MDNode *LocMD = nullptr;
for (const MachineOperand &MO : llvm::reverse(MI->operands())) {
if (MO.isMetadata() && (LocMD = MO.getMetadata()) &&
LocMD->getNumOperands() != 0) {
if (const ConstantInt *CI =
mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
LocCookie = CI->getZExtValue();
break;
}
}
}

// Emit the inline asm to a temporary string so we can emit it through
// EmitInlineAsm.
Expand Down Expand Up @@ -389,23 +397,20 @@ void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
Msg += LS;
Msg += TRI->getRegAsmName(RR);
}

const Function &Fn = MF->getFunction();
const char *Note =
"Reserved registers on the clobber list may not be "
"preserved across the asm statement, and clobbering them may "
"lead to undefined behaviour.";
LLVMContext &Ctx = Fn.getContext();
Ctx.diagnose(DiagnosticInfoInlineAsm(LocCookie, Msg,
DiagnosticSeverity::DS_Warning));
Ctx.diagnose(
MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
LocCookie, Msg, DiagnosticSeverity::DS_Warning));
MMI->getModule()->getContext().diagnose(
DiagnosticInfoInlineAsm(LocCookie, Note, DiagnosticSeverity::DS_Note));

for (const Register RR : RestrRegs) {
if (std::optional<std::string> reason =
TRI->explainReservedReg(*MF, RR)) {
Ctx.diagnose(DiagnosticInfoInlineAsm(LocCookie, *reason,
DiagnosticSeverity::DS_Note));
MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
LocCookie, *reason, DiagnosticSeverity::DS_Note));
}
}
}
Expand Down
32 changes: 11 additions & 21 deletions llvm/lib/CodeGen/MachineInstr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2219,36 +2219,26 @@ MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
return hash_combine_range(HashComponents.begin(), HashComponents.end());
}

const MDNode *MachineInstr::getLocCookieMD() const {
void MachineInstr::emitError(StringRef Msg) const {
// Find the source location cookie.
uint64_t LocCookie = 0;
const MDNode *LocMD = nullptr;
for (unsigned i = getNumOperands(); i != 0; --i) {
if (getOperand(i-1).isMetadata() &&
(LocMD = getOperand(i-1).getMetadata()) &&
LocMD->getNumOperands() != 0) {
if (mdconst::hasa<ConstantInt>(LocMD->getOperand(0)))
return LocMD;
if (const ConstantInt *CI =
mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
LocCookie = CI->getZExtValue();
break;
}
}
}

return nullptr;
}

void MachineInstr::emitInlineAsmError(const Twine &Msg) const {
assert(isInlineAsm());
const MDNode *LocMD = getLocCookieMD();
uint64_t LocCookie =
LocMD
? mdconst::extract<ConstantInt>(LocMD->getOperand(0))->getZExtValue()
: 0;
LLVMContext &Ctx = getMF()->getFunction().getContext();
Ctx.diagnose(DiagnosticInfoInlineAsm(LocCookie, Msg));
}

void MachineInstr::emitGenericError(const Twine &Msg) const {
const Function &Fn = getMF()->getFunction();
Fn.getContext().diagnose(
DiagnosticInfoGenericWithLoc(Msg, Fn, getDebugLoc()));
if (const MachineBasicBlock *MBB = getParent())
if (const MachineFunction *MF = MBB->getParent())
return MF->getFunction().getContext().emitError(LocCookie, Msg);
report_fatal_error(Msg);
}

MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/CodeGen/RegAllocBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ void RegAllocBase::allocatePhysRegs() {
if (AllocOrder.empty())
report_fatal_error("no registers from class available to allocate");
else if (MI && MI->isInlineAsm()) {
MI->emitInlineAsmError(
"inline assembly requires more registers than available");
MI->emitError("inline assembly requires more registers than available");
} else if (MI) {
LLVMContext &Context =
MI->getParent()->getParent()->getFunction().getContext();
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/CodeGen/RegAllocFast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,10 +964,9 @@ void RegAllocFastImpl::allocVirtReg(MachineInstr &MI, LiveReg &LR,
// Nothing we can do: Report an error and keep going with an invalid
// allocation.
if (MI.isInlineAsm())
MI.emitInlineAsmError(
"inline assembly requires more registers than available");
MI.emitError("inline assembly requires more registers than available");
else
MI.emitInlineAsmError("ran out of registers during register allocation");
MI.emitError("ran out of registers during register allocation");

LR.Error = true;
LR.PhysReg = 0;
Expand Down
11 changes: 5 additions & 6 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,13 @@ getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts,
static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V,
const Twine &ErrMsg) {
const Instruction *I = dyn_cast_or_null<Instruction>(V);
if (!I)
if (!V)
return Ctx.emitError(ErrMsg);

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

return Ctx.emitError(I, ErrMsg);
}
Expand Down Expand Up @@ -10504,7 +10503,7 @@ void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
const Twine &Message) {
LLVMContext &Ctx = *DAG.getContext();
Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
Ctx.emitError(&Call, Message);

// Make sure we leave the DAG in a valid state
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Expand Down
9 changes: 2 additions & 7 deletions llvm/lib/CodeGen/XRayInstrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
Expand Down Expand Up @@ -212,12 +211,8 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
auto &FirstMI = *FirstMBB.begin();

if (!MF.getSubtarget().isXRaySupported()) {

const Function &Fn = FirstMBB.getParent()->getFunction();
Fn.getContext().diagnose(DiagnosticInfoUnsupported(
Fn, "An attempt to perform XRay instrumentation for an"
" unsupported target."));

FirstMI.emitError("An attempt to perform XRay instrumentation for an"
" unsupported target.");
return false;
}

Expand Down
14 changes: 0 additions & 14 deletions llvm/lib/IR/DiagnosticInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,6 @@ int llvm::getNextAvailablePluginDiagnosticKind() {

const char *OptimizationRemarkAnalysis::AlwaysPrint = "";

void DiagnosticInfoGeneric::print(DiagnosticPrinter &DP) const {
DP << getMsgStr();
}

void DiagnosticInfoGenericWithLoc::print(DiagnosticPrinter &DP) const {
DP << getLocationStr() << ": " << getMsgStr();
}

DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(uint64_t LocCookie,
const Twine &MsgStr,
DiagnosticSeverity Severity)
: DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
MsgStr(MsgStr) {}

DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
const Twine &MsgStr,
DiagnosticSeverity Severity)
Expand Down
10 changes: 7 additions & 3 deletions llvm/lib/IR/LLVMContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ void LLVMContext::yield() {
}

void LLVMContext::emitError(const Twine &ErrorStr) {
diagnose(DiagnosticInfoGeneric(ErrorStr));
diagnose(DiagnosticInfoInlineAsm(ErrorStr));
}

void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
assert(I && "Invalid instruction");
diagnose(DiagnosticInfoGeneric(I, ErrorStr));
assert (I && "Invalid instruction");
diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
}

static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
Expand Down Expand Up @@ -283,6 +283,10 @@ void LLVMContext::diagnose(const DiagnosticInfo &DI) {
exit(1);
}

void LLVMContext::emitError(uint64_t LocCookie, const Twine &ErrorStr) {
diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
}

//===----------------------------------------------------------------------===//
// Metadata Kind Uniquing
//===----------------------------------------------------------------------===//
Expand Down
Loading
Loading