Skip to content

Commit

Permalink
[mlir] Ensure normalized xls calls name is unique.
Browse files Browse the repository at this point in the history
If one had overlapping file stems or reused function names they could result in duplicate identifiers in symboltable.

PiperOrigin-RevId: 683935796
  • Loading branch information
jpienaar authored and copybara-github committed Oct 9, 2024
1 parent e6932ec commit 7653ea5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 12 additions & 4 deletions xls/contrib/mlir/testdata/normalize_calls.mlir
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
// RUN: xls/contrib/mlir/xls_opt -normalize-xls-calls %s 2>&1 | FileCheck %s

// CHECK: xls.import{{.*}}"xls/contrib/mlir/testdata/dot_product.x"
// CHECK: func.func private{{.*}}fixed_test
// CHECK-LABEL: simple_call
// CHECK: xls.import_dslx_file_package "xls/contrib/mlir/testdata/dot_product.x" as @dot_product
// CHECK: func.func private @dot_product_dot_product_fixed_test(!xls.array<4 x i32>, !xls.array<4 x i32>) -> i32 attributes {xls.linkage = {{.*}}<@dot_product : "dot_product_fixed_test">}
// CHECK: xls.import_dslx_file_package "xls/contrib/mlir/testdata/foo/dot_product.x" as @dot_product_0
// CHECK: func.func private @dot_product_dot_product_fixed_test_1(!xls.array<4 x i32>, !xls.array<4 x i32>) -> i32 attributes {xls.linkage = {{.*}}<@dot_product_0 : "dot_product_fixed_test">}

// CHECK-LABEL: func.func @simple_call(
// CHECK-SAME: %[[VAL_0:.*]]: !xls.array<4 x i32>, %[[VAL_1:.*]]: !xls.array<4 x i32>
// CHECK: %[[VAL_2:.*]] = call @dot_product_dot_product_fixed_test(%[[VAL_0]], %[[VAL_1]])
// CHECK: %[[VAL_3:.*]] = call @dot_product_dot_product_fixed_test_1(%[[VAL_0]], %[[VAL_1]])
// CHECK: return %[[VAL_2]] : i32
func.func @simple_call(%arg0: !xls.array<4 x i32>, %arg1: !xls.array<4 x i32>) -> i32 {
// CHECK: call @{{.*}}dot_product_fixed_test
%0 = xls.call_dslx "xls/contrib/mlir/testdata/dot_product.x":
"dot_product_fixed_test"(%arg0, %arg1) : (!xls.array<4 x i32>, !xls.array<4 x i32>) -> i32
%1 = xls.call_dslx "xls/contrib/mlir/testdata/foo/dot_product.x":
"dot_product_fixed_test"(%arg0, %arg1) : (!xls.array<4 x i32>, !xls.array<4 x i32>) -> i32
return %0 : i32
}
5 changes: 5 additions & 0 deletions xls/contrib/mlir/transforms/normalize_xls_calls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void NormalizeXlsCallsPass::runOnOperation() {
llvm::DenseMap<std::pair<StringRef, StringRef>,
std::vector<mlir::func::FuncOp>>
fnOps;
SymbolTable symbolTable(getOperation());
OpBuilder builder(getOperation().getBodyRegion());
getOperation()->walk([&](Operation* op) {
if (auto import = dyn_cast<ImportDslxFilePackageOp>(op)) {
Expand All @@ -68,6 +69,8 @@ void NormalizeXlsCallsPass::runOnOperation() {
auto pkgImport = builder.create<ImportDslxFilePackageOp>(
op->getLoc(), call.getFilenameAttr(),
builder.getStringAttr(path.stem().string()));
// Ensure unique symbol name.
symbolTable.insert(pkgImport);
it->second.push_back(pkgImport);
}

Expand All @@ -82,6 +85,8 @@ void NormalizeXlsCallsPass::runOnOperation() {
builder.getContext(),
SymbolRefAttr::get(it->second.front()),
call.getFunctionAttr()));
// Ensure unique symbol name.
symbolTable.insert(func);
fIt->second.push_back(func);
}

Expand Down

0 comments on commit 7653ea5

Please sign in to comment.