Skip to content
Draft
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
8 changes: 3 additions & 5 deletions xls/contrib/mlir/IR/xls_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ LogicalResult verifyImportedVerilogFunctionArgumentsAndResults(
<< "for Verilog imported function all arguments should "
"be named (use xls.name attribute)";
}
StringRef attrName = "xls.name";
if (!dictAttr.contains(attrName)) {
if (!dictAttr.contains(kNameAttr)) {
func->emitError() << "for Verilog imported function all arguments"
" should be named (use xls.name attribute)";
return failure();
Expand All @@ -174,8 +173,7 @@ LogicalResult verifyImportedVerilogFunctionArgumentsAndResults(
<< "for Verilog imported function all results should "
"be named (use xls.name attribute)";
}
StringRef attrName = "xls.name";
if (!dictAttr.contains(attrName)) {
if (!dictAttr.contains(kNameAttr)) {
func->emitError() << "for Verilog imported function all results should "
"be named (use xls.name attribute)";
return failure();
Expand Down Expand Up @@ -371,7 +369,7 @@ LogicalResult ImportVerilogFileOp::verifySymbolUses(
if (!func) {
continue;
}
auto linkage = func->getAttrOfType<TranslationLinkage>("xls.linkage");
auto linkage = func->getAttrOfType<TranslationLinkage>(kLinkageAttr);
if (!linkage) {
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions xls/contrib/mlir/IR/xls_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

namespace mlir::xls {

constexpr llvm::StringLiteral kNameAttr = "xls.name";
constexpr llvm::StringLiteral kLinkageAttr = "xls.linkage";

class XlsDialect : public Dialect {
public:
explicit XlsDialect(mlir::MLIRContext* ctx);
Expand Down
10 changes: 7 additions & 3 deletions xls/contrib/mlir/IR/xls_ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,18 @@ def Xls_LinkageKindAttr : EnumAttr<Xls_Dialect, Xls_LinkageKind, "linkage_kind">
def Xls_TranslationLinkage : Xls_Attr<"TranslationLinkage"> {
let summary = "Translation linkage attribute";
let description = [{
Refers to a DSLX package and symbol to resolve too at translation time.
Refers to a DSLX package and symbol to resolve at translation time.
If specified, `delay_ps` overrides the global default delay for a foreign function.
}];
let parameters = (ins
"::mlir::SymbolRefAttr":$package,
"::mlir::StringAttr":$function,
DefaultValuedParameter<"mlir::xls::LinkageKind", "LinkageKind::kImport">:$kind);
DefaultValuedParameter<"mlir::xls::LinkageKind", "LinkageKind::kImport">:$kind,
OptionalParameter<"std::optional<int64_t>">:$delay_ps
);
let mnemonic = "translation_linkage";
let assemblyFormat = "`<` $package `:` $function (`,` $kind^)? `>`";
let assemblyFormat =
"`<` $package `:` $function (`,` $kind^)? (`,` `delay` `=` $delay_ps^)? `>`";
let attrName = "xls.translation_linkage";
}

Expand Down
4 changes: 3 additions & 1 deletion xls/contrib/mlir/testdata/translate_foreign.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
// RUN: xls_translate --mlir-xls-to-verilog %s -- --ffi_fallback_delay_ps=100 --generator=combinational 2>&1 | FileCheck %s --dump-input-filter=all --check-prefix=VERILOG

// XLS: #[ffi_proto("""code_template: "int_to_float {fn}(.x({x}), .out({return}))"
// XLS-NEXT: delay_ps: 1500
// XLS-NEXT: """)]
// XLS: fn __struct_type__int_to_float
// XLS-SAME: x

// VERILOG: int_to_float __struct_type__int_to_float__bitsreturn___invoke_{{.*}}_inst(.x({{.*}}), .out({{.*}}))

xls.import_dslx_file_package "xls/contrib/mlir/testdata/struct_type.x" as @struct_type
func.func private @int_to_float(%a: i32) -> f32 attributes
{xls.linkage = #xls.translation_linkage<@struct_type:"int_to_float", foreign>}
{xls.linkage = #xls.translation_linkage<@struct_type:"int_to_float", foreign, delay = 1500>}
func.func private @float_to_int(%a: f32) -> i32 attributes
{xls.linkage = #xls.translation_linkage<@struct_type:"float_to_int", foreign>}

Expand Down
17 changes: 11 additions & 6 deletions xls/contrib/mlir/tools/xls_translate/xls_translate_from_mlir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,7 @@ bool isVerilogImport(Operation* op, const ModuleOp& module,
XlsRegionOpInterface& xlsRegion) {
bool isImportedVerilog = false;
if (auto func = dyn_cast<FuncOp>(op)) {
auto linkage = func->getAttrOfType<TranslationLinkage>("xls.linkage");
auto linkage = func->getAttrOfType<TranslationLinkage>(kLinkageAttr);
if (linkage) {
Operation* importOp = translationState.getSymbolTable().lookupSymbolIn(
module, linkage.getPackage());
Expand All @@ -1495,11 +1495,10 @@ LogicalResult getArgumentNamesForVerilogImport(
::std::optional<ArrayAttr> argAttrs = func.getArgAttrs();
const Region::BlockArgListType& argValues =
xls_region.getBodyRegion().getArguments();
StringRef attrName = "xls.name";
ArrayRef<Attribute> attrArgs = argAttrs->getValue();
for (auto [argValue, attr] : zip(argValues, attrArgs)) {
auto dictAttr = dyn_cast<DictionaryAttr>(attr);
auto nameAttr = dictAttr.getNamed(attrName);
auto nameAttr = dictAttr.getNamed(kNameAttr);
::xls::Type* xls_type = translation_state.getType(argValue.getType());
if (!xls_type) {
return failure();
Expand Down Expand Up @@ -1546,7 +1545,7 @@ LogicalResult annotateForeignFunction(FuncOp func, ::xls::Function& xlsFunc) {
return func->emitError() << "there should be one result when importing"
" a verilog function";
}
auto linkage = func->getAttrOfType<TranslationLinkage>("xls.linkage");
auto linkage = func->getAttrOfType<TranslationLinkage>(kLinkageAttr);
if (!linkage || linkage.getKind() != LinkageKind::kForeign) {
return func->emitError() << "expected foreign xls.linkage attribute";
}
Expand All @@ -1560,7 +1559,7 @@ LogicalResult annotateForeignFunction(FuncOp func, ::xls::Function& xlsFunc) {
if (!dictAttr) {
continue;
}
std::optional<NamedAttribute> namedAttr = dictAttr.getNamed("xls.name");
std::optional<NamedAttribute> namedAttr = dictAttr.getNamed(kNameAttr);
if (namedAttr != std::nullopt) {
auto nameAttr = cast<StringAttr>(namedAttr->getValue());
resultName = nameAttr.getValue();
Expand All @@ -1583,6 +1582,9 @@ LogicalResult annotateForeignFunction(FuncOp func, ::xls::Function& xlsFunc) {
return failure();
}

if (linkage.getDelayPs().has_value()) {
ffd->set_delay_ps(linkage.getDelayPs().value());
}
xlsFunc.SetForeignFunctionData(ffd.value());
return success();
}
Expand Down Expand Up @@ -1942,7 +1944,7 @@ FailureOr<std::unique_ptr<Package>> mlirXlsToXls(Operation* op,
isVerilogImport(&op, module, translation_state, xls_region);
// Skip function declarations for now.
if (auto func = dyn_cast<FuncOp>(op); func && func.isDeclaration()) {
auto linkage = func->getAttrOfType<TranslationLinkage>("xls.linkage");
auto linkage = func->getAttrOfType<TranslationLinkage>(kLinkageAttr);
if (!linkage) {
continue;
}
Expand Down Expand Up @@ -1981,6 +1983,9 @@ FailureOr<std::unique_ptr<Package>> mlirXlsToXls(Operation* op,
<< ffd.status().message();
return failure();
}
if (linkage.getDelayPs().has_value()) {
ffd->set_delay_ps(linkage.getDelayPs().value());
}
xlsFunc.value()->SetForeignFunctionData(ffd.value());
}
continue;
Expand Down
9 changes: 6 additions & 3 deletions xls/contrib/mlir/transforms/arith_to_xls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
Expand Down Expand Up @@ -54,18 +56,19 @@ FuncOp maybeDeclareDslxFn(SymbolTable& symtab, OpBuilder builder,
const std::string& symbolName,
const std::string& dslxName,
xls::ImportDslxFilePackageOp importOp,
TypeRange results, TypeRange operands) {
TypeRange results, TypeRange operands,
std::optional<int64_t> delayPs = {}) {
if (auto fn = symtab.lookup<FuncOp>(symbolName)) {
return fn;
}

FuncOp fn = FuncOp::create(builder, importOp->getLoc(), symbolName,
builder.getFunctionType(operands, results), {});
fn.setVisibility(SymbolTable::Visibility::Private);
fn->setAttr("xls.linkage",
fn->setAttr(kLinkageAttr,
xls::TranslationLinkage::get(
builder.getContext(), SymbolRefAttr::get(importOp),
builder.getStringAttr(dslxName), /*kind=*/{}));
builder.getStringAttr(dslxName), /*kind=*/{}, delayPs));
return fn;
}

Expand Down
9 changes: 5 additions & 4 deletions xls/contrib/mlir/transforms/normalize_xls_calls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ void NormalizeXlsCallsPass::runOnOperation() {
llvm::formatv("{0}_{1}", path.stem(), call.getFunction()).str(),
newType);
func.setVisibility(SymbolTable::Visibility::Private);
func->setAttr("xls.linkage", xls::TranslationLinkage::get(
builder.getContext(),
SymbolRefAttr::get(it->second.front()),
call.getFunctionAttr(), /*kind=*/{}));
func->setAttr(kLinkageAttr, xls::TranslationLinkage::get(
builder.getContext(),
SymbolRefAttr::get(it->second.front()),
call.getFunctionAttr(), /*kind=*/{},
/*delay_ps=*/{}));
// Ensure unique symbol name.
symbolTable.insert(func);
fIt->second.push_back(func);
Expand Down
Loading