Skip to content

Commit e4234d6

Browse files
committed
better error reporting and nits
1 parent 00ecb24 commit e4234d6

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ class ModuleTranslation {
230230

231231
/// Translates parameter attributes of a call and adds them to the returned
232232
/// AttrBuilder. Returns failure if any of the translations failed.
233-
FailureOr<llvm::AttrBuilder> convertParameterAttrs(DictionaryAttr paramAttrs);
233+
FailureOr<llvm::AttrBuilder> convertParameterAttrs(CallOpInterface callOp,
234+
DictionaryAttr paramAttrs);
234235

235236
/// Gets the named metadata in the LLVM IR module being constructed, creating
236237
/// it if it does not exist.

mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ convertParameterAndResultAttrs(CallOpInterface callOp, llvm::CallBase *call,
229229
LLVM::ModuleTranslation &moduleTranslation) {
230230
if (ArrayAttr argAttrsArray = callOp.getArgAttrsAttr()) {
231231
for (auto [argIdx, argAttrsAttr] : llvm::enumerate(argAttrsArray)) {
232-
if (auto argAttrs = llvm::cast<DictionaryAttr>(argAttrsAttr)) {
232+
if (auto argAttrs = cast<DictionaryAttr>(argAttrsAttr);
233+
!argAttrs.empty()) {
233234
FailureOr<llvm::AttrBuilder> attrBuilder =
234-
moduleTranslation.convertParameterAttrs(argAttrs);
235+
moduleTranslation.convertParameterAttrs(callOp, argAttrs);
235236
if (failed(attrBuilder))
236237
return failure();
237238
call->addParamAttrs(argIdx, *attrBuilder);
@@ -240,10 +241,14 @@ convertParameterAndResultAttrs(CallOpInterface callOp, llvm::CallBase *call,
240241
}
241242

242243
ArrayAttr resAttrsArray = callOp.getResAttrsAttr();
243-
if (resAttrsArray && resAttrsArray.size() == 1) {
244-
if (auto resAttrs = llvm::cast<DictionaryAttr>(resAttrsArray[0])) {
244+
if (resAttrsArray && resAttrsArray.size() > 0) {
245+
if (resAttrsArray.size() != 1)
246+
return mlir::emitError(callOp.getLoc(),
247+
"llvm.func cannot have multiple results");
248+
if (auto resAttrs = cast<DictionaryAttr>(resAttrsArray[0]);
249+
!resAttrs.empty()) {
245250
FailureOr<llvm::AttrBuilder> attrBuilder =
246-
moduleTranslation.convertParameterAttrs(resAttrs);
251+
moduleTranslation.convertParameterAttrs(callOp, resAttrs);
247252
if (failed(attrBuilder))
248253
return failure();
249254
call->addRetAttrs(*attrBuilder);

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,7 +2157,7 @@ void ModuleImport::convertParameterAttributes(llvm::Function *func,
21572157
void ModuleImport::convertParameterAttributes(llvm::CallBase *call,
21582158
CallOpInterface callOp,
21592159
OpBuilder &builder) {
2160-
auto llvmAttrs = call->getAttributes();
2160+
llvm::AttributeList llvmAttrs = call->getAttributes();
21612161
SmallVector<llvm::AttributeSet> llvmArgAttrsSet;
21622162
bool anyArgAttrs = false;
21632163
for (size_t i = 0, e = call->arg_size(); i < e; ++i) {
@@ -2181,9 +2181,8 @@ void ModuleImport::convertParameterAttributes(llvm::CallBase *call,
21812181
llvm::AttributeSet llvmResAttr = llvmAttrs.getRetAttrs();
21822182
if (!llvmResAttr.hasAttributes())
21832183
return;
2184-
SmallVector<DictionaryAttr, 1> resAttrs;
2185-
resAttrs.emplace_back(convertParameterAttribute(llvmResAttr, builder));
2186-
callOp.setResAttrsAttr(getArrayAttr(resAttrs));
2184+
DictionaryAttr resAttrs = convertParameterAttribute(llvmResAttr, builder);
2185+
callOp.setResAttrsAttr(getArrayAttr({resAttrs}));
21872186
}
21882187

21892188
template <typename Op>

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,23 +1563,33 @@ static void convertFunctionKernelAttributes(LLVMFuncOp func,
15631563
}
15641564
}
15651565

1566-
static void convertParameterAttr(llvm::AttrBuilder &attrBuilder,
1567-
llvm::Attribute::AttrKind llvmKind,
1568-
NamedAttribute namedAttr,
1569-
ModuleTranslation &moduleTranslation) {
1570-
llvm::TypeSwitch<Attribute>(namedAttr.getValue())
1566+
static LogicalResult convertParameterAttr(llvm::AttrBuilder &attrBuilder,
1567+
llvm::Attribute::AttrKind llvmKind,
1568+
NamedAttribute namedAttr,
1569+
ModuleTranslation &moduleTranslation,
1570+
Location loc) {
1571+
return llvm::TypeSwitch<Attribute, LogicalResult>(namedAttr.getValue())
15711572
.Case<TypeAttr>([&](auto typeAttr) {
15721573
attrBuilder.addTypeAttr(
15731574
llvmKind, moduleTranslation.convertType(typeAttr.getValue()));
1575+
return success();
15741576
})
15751577
.Case<IntegerAttr>([&](auto intAttr) {
15761578
attrBuilder.addRawIntAttr(llvmKind, intAttr.getInt());
1579+
return success();
1580+
})
1581+
.Case<UnitAttr>([&](auto) {
1582+
attrBuilder.addAttribute(llvmKind);
1583+
return success();
15771584
})
1578-
.Case<UnitAttr>([&](auto) { attrBuilder.addAttribute(llvmKind); })
15791585
.Case<LLVM::ConstantRangeAttr>([&](auto rangeAttr) {
15801586
attrBuilder.addConstantRangeAttr(
15811587
llvmKind,
15821588
llvm::ConstantRange(rangeAttr.getLower(), rangeAttr.getUpper()));
1589+
return success();
1590+
})
1591+
.Default([loc](auto) {
1592+
return emitError(loc, "unsupported parameter attribute type");
15831593
});
15841594
}
15851595

@@ -1588,12 +1598,15 @@ ModuleTranslation::convertParameterAttrs(LLVMFuncOp func, int argIdx,
15881598
DictionaryAttr paramAttrs) {
15891599
llvm::AttrBuilder attrBuilder(llvmModule->getContext());
15901600
auto attrNameToKindMapping = getAttrNameToKindMapping();
1601+
Location loc = func.getLoc();
15911602

15921603
for (auto namedAttr : paramAttrs) {
15931604
auto it = attrNameToKindMapping.find(namedAttr.getName());
15941605
if (it != attrNameToKindMapping.end()) {
15951606
llvm::Attribute::AttrKind llvmKind = it->second;
1596-
convertParameterAttr(attrBuilder, llvmKind, namedAttr, *this);
1607+
if (failed(convertParameterAttr(attrBuilder, llvmKind, namedAttr, *this,
1608+
loc)))
1609+
return failure();
15971610
} else if (namedAttr.getNameDialect()) {
15981611
if (failed(iface.convertParameterAttr(func, argIdx, namedAttr, *this)))
15991612
return failure();
@@ -1604,15 +1617,19 @@ ModuleTranslation::convertParameterAttrs(LLVMFuncOp func, int argIdx,
16041617
}
16051618

16061619
FailureOr<llvm::AttrBuilder>
1607-
ModuleTranslation::convertParameterAttrs(DictionaryAttr paramAttrs) {
1620+
ModuleTranslation::convertParameterAttrs(CallOpInterface callOp,
1621+
DictionaryAttr paramAttrs) {
16081622
llvm::AttrBuilder attrBuilder(llvmModule->getContext());
1623+
Location loc = callOp.getLoc();
16091624
auto attrNameToKindMapping = getAttrNameToKindMapping();
16101625

16111626
for (auto namedAttr : paramAttrs) {
16121627
auto it = attrNameToKindMapping.find(namedAttr.getName());
16131628
if (it != attrNameToKindMapping.end()) {
16141629
llvm::Attribute::AttrKind llvmKind = it->second;
1615-
convertParameterAttr(attrBuilder, llvmKind, namedAttr, *this);
1630+
if (failed(convertParameterAttr(attrBuilder, llvmKind, namedAttr, *this,
1631+
loc)))
1632+
return failure();
16161633
}
16171634
}
16181635

0 commit comments

Comments
 (0)