Changeset View
Changeset View
Standalone View
Standalone View
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
Show First 20 Lines • Show All 890 Lines • ▼ Show 20 Lines | newMemEffects |= llvm::MemoryEffects( | ||||
llvm::MemoryEffects::Location::InaccessibleMem, | llvm::MemoryEffects::Location::InaccessibleMem, | ||||
convertModRefInfoToLLVM(memEffects.getInaccessibleMem())); | convertModRefInfoToLLVM(memEffects.getInaccessibleMem())); | ||||
newMemEffects |= | newMemEffects |= | ||||
llvm::MemoryEffects(llvm::MemoryEffects::Location::Other, | llvm::MemoryEffects(llvm::MemoryEffects::Location::Other, | ||||
convertModRefInfoToLLVM(memEffects.getOther())); | convertModRefInfoToLLVM(memEffects.getOther())); | ||||
llvmFunc->setMemoryEffects(newMemEffects); | llvmFunc->setMemoryEffects(newMemEffects); | ||||
} | } | ||||
llvm::AttrBuilder | |||||
ModuleTranslation::convertParameterAttrs(DictionaryAttr paramAttrs) { | |||||
llvm::AttrBuilder attrBuilder(llvmModule->getContext()); | |||||
if (auto attr = paramAttrs.getAs<UnitAttr>(LLVMDialect::getNoAliasAttrName())) | |||||
gysit: This seems to be dead code? | |||||
attrBuilder.addAttribute(llvm::Attribute::AttrKind::NoAlias); | |||||
if (auto attr = | |||||
paramAttrs.getAs<UnitAttr>(LLVMDialect::getReadonlyAttrName())) | |||||
attrBuilder.addAttribute(llvm::Attribute::AttrKind::ReadOnly); | |||||
if (auto attr = | |||||
paramAttrs.getAs<IntegerAttr>(LLVMDialect::getAlignAttrName())) | |||||
attrBuilder.addAlignmentAttr(llvm::Align(attr.getInt())); | |||||
if (auto attr = | |||||
paramAttrs.getAs<TypeAttr>(LLVMDialect::getStructRetAttrName())) | |||||
attrBuilder.addStructRetAttr(convertType(attr.getValue())); | |||||
if (auto attr = paramAttrs.getAs<TypeAttr>(LLVMDialect::getByValAttrName())) | |||||
attrBuilder.addByValAttr(convertType(attr.getValue())); | |||||
if (auto attr = paramAttrs.getAs<TypeAttr>(LLVMDialect::getByRefAttrName())) | |||||
attrBuilder.addByRefAttr(convertType(attr.getValue())); | |||||
if (auto attr = | |||||
paramAttrs.getAs<TypeAttr>(LLVMDialect::getInAllocaAttrName())) | |||||
attrBuilder.addInAllocaAttr(convertType(attr.getValue())); | |||||
if (auto attr = paramAttrs.getAs<UnitAttr>(LLVMDialect::getNestAttrName())) | |||||
attrBuilder.addAttribute(llvm::Attribute::Nest); | |||||
if (auto attr = paramAttrs.getAs<UnitAttr>(LLVMDialect::getNoUndefAttrName())) | |||||
attrBuilder.addAttribute(llvm::Attribute::NoUndef); | |||||
if (auto attr = paramAttrs.getAs<UnitAttr>(LLVMDialect::getSExtAttrName())) | |||||
attrBuilder.addAttribute(llvm::Attribute::SExt); | |||||
if (auto attr = paramAttrs.getAs<UnitAttr>(LLVMDialect::getZExtAttrName())) | |||||
attrBuilder.addAttribute(llvm::Attribute::ZExt); | |||||
return attrBuilder; | |||||
} | |||||
LogicalResult ModuleTranslation::convertFunctionSignatures() { | LogicalResult ModuleTranslation::convertFunctionSignatures() { | ||||
// Declare all functions first because there may be function calls that form a | // Declare all functions first because there may be function calls that form a | ||||
// call graph with cycles, or global initializers that reference functions. | // call graph with cycles, or global initializers that reference functions. | ||||
for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { | for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { | ||||
llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction( | llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction( | ||||
function.getName(), | function.getName(), | ||||
cast<llvm::FunctionType>(convertType(function.getFunctionType()))); | cast<llvm::FunctionType>(convertType(function.getFunctionType()))); | ||||
llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee()); | llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee()); | ||||
llvmFunc->setLinkage(convertLinkageToLLVM(function.getLinkage())); | llvmFunc->setLinkage(convertLinkageToLLVM(function.getLinkage())); | ||||
llvmFunc->setCallingConv(convertCConvToLLVM(function.getCConv())); | llvmFunc->setCallingConv(convertCConvToLLVM(function.getCConv())); | ||||
mapFunction(function.getName(), llvmFunc); | mapFunction(function.getName(), llvmFunc); | ||||
addRuntimePreemptionSpecifier(function.getDsoLocal(), llvmFunc); | addRuntimePreemptionSpecifier(function.getDsoLocal(), llvmFunc); | ||||
// Convert function attributes. | // Convert function attributes. | ||||
convertFunctionAttributes(function, llvmFunc); | convertFunctionAttributes(function, llvmFunc); | ||||
// Convert function_entry_count attribute to metadata. | // Convert function_entry_count attribute to metadata. | ||||
if (std::optional<uint64_t> entryCount = function.getFunctionEntryCount()) | if (std::optional<uint64_t> entryCount = function.getFunctionEntryCount()) | ||||
llvmFunc->setEntryCount(entryCount.value()); | llvmFunc->setEntryCount(entryCount.value()); | ||||
// Convert result attributes. | // Convert result attributes. | ||||
if (ArrayAttr allResultAttrs = function.getAllResultAttrs()) { | if (ArrayAttr allResultAttrs = function.getAllResultAttrs()) { | ||||
llvm::AttrBuilder retAttrs(llvmFunc->getContext()); | |||||
DictionaryAttr resultAttrs = allResultAttrs[0].cast<DictionaryAttr>(); | DictionaryAttr resultAttrs = allResultAttrs[0].cast<DictionaryAttr>(); | ||||
for (const NamedAttribute &attr : resultAttrs) { | llvmFunc->addRetAttrs(convertParameterAttrs(resultAttrs)); | ||||
StringAttr name = attr.getName(); | |||||
if (name == LLVMDialect::getAlignAttrName()) { | |||||
auto alignAmount = attr.getValue().cast<IntegerAttr>(); | |||||
retAttrs.addAlignmentAttr(llvm::Align(alignAmount.getInt())); | |||||
} else if (name == LLVMDialect::getNoAliasAttrName()) { | |||||
retAttrs.addAttribute(llvm::Attribute::NoAlias); | |||||
} else if (name == LLVMDialect::getNoUndefAttrName()) { | |||||
retAttrs.addAttribute(llvm::Attribute::NoUndef); | |||||
} else if (name == LLVMDialect::getSExtAttrName()) { | |||||
retAttrs.addAttribute(llvm::Attribute::SExt); | |||||
} else if (name == LLVMDialect::getZExtAttrName()) { | |||||
retAttrs.addAttribute(llvm::Attribute::ZExt); | |||||
} | |||||
} | |||||
llvmFunc->addRetAttrs(retAttrs); | |||||
} | } | ||||
// Convert argument attributes. | // Convert argument attributes. | ||||
unsigned int argIdx = 0; | for (auto [argIdx, llvmArg] : llvm::enumerate(llvmFunc->args())) { | ||||
for (auto [mlirArgTy, llvmArg] : | if (DictionaryAttr argAttrs = function.getArgAttrDict(argIdx)) { | ||||
llvm::zip(function.getArgumentTypes(), llvmFunc->args())) { | llvm::AttrBuilder attrBuilder = convertParameterAttrs(argAttrs); | ||||
nit: if it fits one line I would inline the getArgAttrDict call into the condition. gysit: nit: if it fits one line I would inline the getArgAttrDict call into the condition. | |||||
if (auto attr = function.getArgAttrOfType<UnitAttr>( | llvmArg.addAttrs(attrBuilder); | ||||
argIdx, LLVMDialect::getNoAliasAttrName())) { | |||||
// NB: Attribute already verified to be boolean, so check if we can | |||||
// indeed attach the attribute to this argument, based on its type. | |||||
if (!mlirArgTy.isa<LLVM::LLVMPointerType>()) | |||||
return function.emitError( | |||||
"llvm.noalias attribute attached to LLVM non-pointer argument"); | |||||
llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias); | |||||
} | |||||
if (auto attr = function.getArgAttrOfType<UnitAttr>( | |||||
argIdx, LLVMDialect::getReadonlyAttrName())) { | |||||
if (!mlirArgTy.isa<LLVM::LLVMPointerType>()) | |||||
return function.emitError( | |||||
"llvm.readonly attribute attached to LLVM non-pointer argument"); | |||||
llvmArg.addAttr(llvm::Attribute::AttrKind::ReadOnly); | |||||
} | |||||
if (auto attr = function.getArgAttrOfType<IntegerAttr>( | |||||
argIdx, LLVMDialect::getAlignAttrName())) { | |||||
// NB: Attribute already verified to be int, so check if we can indeed | |||||
// attach the attribute to this argument, based on its type. | |||||
if (!mlirArgTy.isa<LLVM::LLVMPointerType>()) | |||||
return function.emitError( | |||||
"llvm.align attribute attached to LLVM non-pointer argument"); | |||||
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()) | |||||
.addAlignmentAttr(llvm::Align(attr.getInt()))); | |||||
} | |||||
if (auto attr = function.getArgAttrOfType<TypeAttr>( | |||||
argIdx, LLVMDialect::getStructRetAttrName())) { | |||||
auto argTy = mlirArgTy.dyn_cast<LLVM::LLVMPointerType>(); | |||||
if (!argTy) | |||||
return function.emitError( | |||||
"llvm.sret attribute attached to LLVM non-pointer argument"); | |||||
if (!argTy.isOpaque() && argTy.getElementType() != attr.getValue()) | |||||
return function.emitError( | |||||
"llvm.sret attribute attached to LLVM pointer " | |||||
"argument of a different type"); | |||||
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()) | |||||
.addStructRetAttr(convertType(attr.getValue()))); | |||||
} | |||||
if (auto attr = function.getArgAttrOfType<TypeAttr>( | |||||
argIdx, LLVMDialect::getByValAttrName())) { | |||||
auto argTy = mlirArgTy.dyn_cast<LLVM::LLVMPointerType>(); | |||||
if (!argTy) | |||||
return function.emitError( | |||||
"llvm.byval attribute attached to LLVM non-pointer argument"); | |||||
if (!argTy.isOpaque() && argTy.getElementType() != attr.getValue()) | |||||
return function.emitError( | |||||
"llvm.byval attribute attached to LLVM pointer " | |||||
"argument of a different type"); | |||||
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()) | |||||
.addByValAttr(convertType(attr.getValue()))); | |||||
} | |||||
if (auto attr = function.getArgAttrOfType<TypeAttr>( | |||||
argIdx, LLVMDialect::getByRefAttrName())) { | |||||
auto argTy = mlirArgTy.dyn_cast<LLVM::LLVMPointerType>(); | |||||
if (!argTy) | |||||
return function.emitError( | |||||
"llvm.byref attribute attached to LLVM non-pointer argument"); | |||||
if (!argTy.isOpaque() && argTy.getElementType() != attr.getValue()) | |||||
return function.emitError( | |||||
"llvm.byref attribute attached to LLVM pointer " | |||||
"argument of a different type"); | |||||
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()) | |||||
.addByRefAttr(convertType(attr.getValue()))); | |||||
} | |||||
if (auto attr = function.getArgAttrOfType<TypeAttr>( | |||||
argIdx, LLVMDialect::getInAllocaAttrName())) { | |||||
auto argTy = mlirArgTy.dyn_cast<LLVM::LLVMPointerType>(); | |||||
if (!argTy) | |||||
return function.emitError( | |||||
"llvm.inalloca attribute attached to LLVM non-pointer argument"); | |||||
if (!argTy.isOpaque() && argTy.getElementType() != attr.getValue()) | |||||
return function.emitError( | |||||
"llvm.inalloca attribute attached to LLVM pointer " | |||||
"argument of a different type"); | |||||
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()) | |||||
.addInAllocaAttr(convertType(attr.getValue()))); | |||||
} | } | ||||
if (auto attr = | |||||
function.getArgAttrOfType<UnitAttr>(argIdx, "llvm.nest")) { | |||||
if (!mlirArgTy.isa<LLVM::LLVMPointerType>()) | |||||
return function.emitError( | |||||
"llvm.nest attribute attached to LLVM non-pointer argument"); | |||||
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()) | |||||
.addAttribute(llvm::Attribute::Nest)); | |||||
} | |||||
if (auto attr = function.getArgAttrOfType<UnitAttr>( | |||||
argIdx, LLVMDialect::getNoUndefAttrName())) { | |||||
// llvm.noundef can be added to any argument type. | |||||
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()) | |||||
.addAttribute(llvm::Attribute::NoUndef)); | |||||
} | |||||
if (auto attr = function.getArgAttrOfType<UnitAttr>( | |||||
argIdx, LLVMDialect::getSExtAttrName())) { | |||||
// llvm.signext can be added to any integer argument type. | |||||
if (!mlirArgTy.isa<mlir::IntegerType>()) | |||||
return function.emitError( | |||||
"llvm.signext attribute attached to LLVM non-integer argument"); | |||||
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()) | |||||
.addAttribute(llvm::Attribute::SExt)); | |||||
} | |||||
if (auto attr = function.getArgAttrOfType<UnitAttr>( | |||||
argIdx, LLVMDialect::getZExtAttrName())) { | |||||
// llvm.zeroext can be added to any integer argument type. | |||||
if (!mlirArgTy.isa<mlir::IntegerType>()) | |||||
return function.emitError( | |||||
"llvm.zeroext attribute attached to LLVM non-integer argument"); | |||||
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()) | |||||
.addAttribute(llvm::Attribute::ZExt)); | |||||
} | |||||
++argIdx; | |||||
} | } | ||||
// Forward the pass-through attributes to LLVM. | // Forward the pass-through attributes to LLVM. | ||||
if (failed(forwardPassthroughAttributes( | if (failed(forwardPassthroughAttributes( | ||||
function.getLoc(), function.getPassthrough(), llvmFunc))) | function.getLoc(), function.getPassthrough(), llvmFunc))) | ||||
return failure(); | return failure(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 364 Lines • Show Last 20 Lines |
This seems to be dead code?