Skip to content

Commit

Permalink
[SystemZ] Dump function signature on missing arg extension. (#109699)
Browse files Browse the repository at this point in the history
Make it easier to handle detected problems by providing the function
signature(s) involved in cases of missing argument extensions.
  • Loading branch information
JonPsson1 authored Sep 30, 2024
1 parent 93af9d6 commit f9fbfc5
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 23 deletions.
78 changes: 63 additions & 15 deletions llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,11 +1925,7 @@ SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
IsTailCall = false;

// Integer args <=32 bits should have an extension attribute.
bool IsInternal = false;
if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee))
if (const Function *Fn = dyn_cast<Function>(G->getGlobal()))
IsInternal = isFullyInternal(Fn);
verifyNarrowIntegerArgs(Outs, IsInternal);
verifyNarrowIntegerArgs_Call(Outs, &MF.getFunction(), Callee);

// Analyze the operands of the call, assigning locations to each operand.
SmallVector<CCValAssign, 16> ArgLocs;
Expand Down Expand Up @@ -2192,7 +2188,7 @@ SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
MachineFunction &MF = DAG.getMachineFunction();

// Integer args <=32 bits should have an extension attribute.
verifyNarrowIntegerArgs(Outs, isFullyInternal(&MF.getFunction()));
verifyNarrowIntegerArgs_Ret(Outs, &MF.getFunction());

// Assign locations to each returned value.
SmallVector<CCValAssign, 16> RetLocs;
Expand Down Expand Up @@ -9835,34 +9831,86 @@ bool SystemZTargetLowering::isFullyInternal(const Function *Fn) const {
return true;
}

// Verify that narrow integer arguments are extended as required by the ABI.
static void printFunctionArgExts(const Function *F, raw_fd_ostream &OS) {
FunctionType *FT = F->getFunctionType();
const AttributeList &Attrs = F->getAttributes();
if (Attrs.hasRetAttrs())
OS << Attrs.getAsString(AttributeList::ReturnIndex) << " ";
OS << *F->getReturnType() << " @" << F->getName() << "(";
for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
if (I)
OS << ", ";
OS << *FT->getParamType(I);
AttributeSet ArgAttrs = Attrs.getParamAttrs(I);
for (auto A : {Attribute::SExt, Attribute::ZExt, Attribute::NoExt})
if (ArgAttrs.hasAttribute(A))
OS << " " << Attribute::getNameFromAttrKind(A);
}
OS << ")\n";
}

void SystemZTargetLowering::
verifyNarrowIntegerArgs_Call(const SmallVectorImpl<ISD::OutputArg> &Outs,
const Function *F, SDValue Callee) const {
bool IsInternal = false;
const Function *CalleeFn = nullptr;
if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee))
if (CalleeFn = dyn_cast<Function>(G->getGlobal()))
IsInternal = isFullyInternal(CalleeFn);
if (!verifyNarrowIntegerArgs(Outs, IsInternal)) {
errs() << "ERROR: Missing extension attribute of passed "
<< "value in call to function:\n" << "Callee: ";
if (CalleeFn != nullptr)
printFunctionArgExts(CalleeFn, errs());
else
errs() << "-";
errs() << "Caller: ";
printFunctionArgExts(F, errs());
llvm_unreachable("");
}
}

void SystemZTargetLowering::
verifyNarrowIntegerArgs_Ret(const SmallVectorImpl<ISD::OutputArg> &Outs,
const Function *F) const {
if (!verifyNarrowIntegerArgs(Outs, isFullyInternal(F))) {
errs() << "ERROR: Missing extension attribute of returned "
<< "value from function:\n";
printFunctionArgExts(F, errs());
llvm_unreachable("");
}
}

// Verify that narrow integer arguments are extended as required by the ABI.
// Return false if an error is found.
bool SystemZTargetLowering::
verifyNarrowIntegerArgs(const SmallVectorImpl<ISD::OutputArg> &Outs,
bool IsInternal) const {
if (IsInternal || !Subtarget.isTargetELF())
return;
return true;

// Temporarily only do the check when explicitly requested, until it can be
// enabled by default.
if (!EnableIntArgExtCheck)
return;
return true;

if (EnableIntArgExtCheck.getNumOccurrences()) {
if (!EnableIntArgExtCheck)
return;
return true;
} else if (!getTargetMachine().Options.VerifyArgABICompliance)
return;
return true;

for (unsigned i = 0; i < Outs.size(); ++i) {
MVT VT = Outs[i].VT;
ISD::ArgFlagsTy Flags = Outs[i].Flags;
if (VT.isInteger()) {
assert((VT == MVT::i32 || VT.getSizeInBits() >= 64) &&
"Unexpected integer argument VT.");
assert((VT != MVT::i32 ||
(Flags.isSExt() || Flags.isZExt() || Flags.isNoExt())) &&
"Narrow integer argument must have a valid extension type.");
(void)Flags;
if (VT == MVT::i32 &&
!Flags.isSExt() && !Flags.isZExt() && !Flags.isNoExt())
return false;
}
}

return true;
}
6 changes: 5 additions & 1 deletion llvm/lib/Target/SystemZ/SystemZISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,11 @@ class SystemZTargetLowering : public TargetLowering {
const TargetRegisterClass *getRepRegClassFor(MVT VT) const override;

bool isFullyInternal(const Function *Fn) const;
void verifyNarrowIntegerArgs(const SmallVectorImpl<ISD::OutputArg> &Outs,
void verifyNarrowIntegerArgs_Call(const SmallVectorImpl<ISD::OutputArg> &Outs,
const Function *F, SDValue Callee) const;
void verifyNarrowIntegerArgs_Ret(const SmallVectorImpl<ISD::OutputArg> &Outs,
const Function *F) const;
bool verifyNarrowIntegerArgs(const SmallVectorImpl<ISD::OutputArg> &Outs,
bool IsInternal) const;
};

Expand Down
4 changes: 3 additions & 1 deletion llvm/test/CodeGen/SystemZ/args-15.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ define i32 @callee_MissingRetAttr() {
ret i32 -1
}

; CHECK: Narrow integer argument must have a valid extension type.
; CHECK: ERROR: Missing extension attribute of returned value from function:
; CHECK: i32 @callee_MissingRetAttr()
; CHECK: UNREACHABLE executed
4 changes: 3 additions & 1 deletion llvm/test/CodeGen/SystemZ/args-16.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ define i16 @callee_MissingRetAttr() {
ret i16 -1
}

; CHECK: Narrow integer argument must have a valid extension type.
; CHECK: ERROR: Missing extension attribute of returned value from function:
; CHECK: i16 @callee_MissingRetAttr()
; CHECK: UNREACHABLE executed

4 changes: 3 additions & 1 deletion llvm/test/CodeGen/SystemZ/args-17.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ define i8 @callee_MissingRetAttr() {
ret i8 -1
}

; CHECK: Narrow integer argument must have a valid extension type.
; CHECK: ERROR: Missing extension attribute of returned value from function:
; CHECK: i8 @callee_MissingRetAttr()
; CHECK: UNREACHABLE executed
4 changes: 3 additions & 1 deletion llvm/test/CodeGen/SystemZ/args-18.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ define void @caller() {

declare void @bar_Struct(i32 %Arg)

; CHECK: Narrow integer argument must have a valid extension type
; CHECK: ERROR: Missing extension attribute of passed value in call to function:
; CHECK: Callee: void @bar_Struct(i32)
; CHECK: Caller: void @caller()
4 changes: 3 additions & 1 deletion llvm/test/CodeGen/SystemZ/args-19.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ define void @caller() {

declare void @bar_Struct(i16 %Arg)

; CHECK: Narrow integer argument must have a valid extension type
; CHECK: ERROR: Missing extension attribute of passed value in call to function:
; CHECK: Callee: void @bar_Struct(i16)
; CHECK: Caller: void @caller()
4 changes: 3 additions & 1 deletion llvm/test/CodeGen/SystemZ/args-20.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ define void @caller() {

declare void @bar_Struct(i8 %Arg)

; CHECK: Narrow integer argument must have a valid extension type
; CHECK: ERROR: Missing extension attribute of passed value in call to function:
; CHECK: Callee: void @bar_Struct(i8)
; CHECK: Caller: void @caller()
4 changes: 3 additions & 1 deletion llvm/test/CodeGen/SystemZ/args-21.ll
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ define void @foo() {
ret void
}

; CHECK: Narrow integer argument must have a valid extension type
; CHECK: ERROR: Missing extension attribute of returned value from function:
; CHECK: i32 @bar(i32)
; CHECK: UNREACHABLE executed

0 comments on commit f9fbfc5

Please sign in to comment.