From 4fff09eae41d9d0a844517ca4cede49edd327f00 Mon Sep 17 00:00:00 2001 From: andrej Date: Fri, 6 Sep 2024 11:03:27 -0600 Subject: [PATCH 1/4] fix assertion error caused by region simplification --- .../AIEX/Transforms/AIESubstituteShimDMAAllocations.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/Dialect/AIEX/Transforms/AIESubstituteShimDMAAllocations.cpp b/lib/Dialect/AIEX/Transforms/AIESubstituteShimDMAAllocations.cpp index 7efe809e36..3db6a926e6 100644 --- a/lib/Dialect/AIEX/Transforms/AIESubstituteShimDMAAllocations.cpp +++ b/lib/Dialect/AIEX/Transforms/AIESubstituteShimDMAAllocations.cpp @@ -67,13 +67,12 @@ struct AIESubstituteShimDMAAllocationsPass // Convert DMAConfigureTaskForOps that reference shim DMA allocations // to regular DMAConfigureTaskOps - ConversionTarget target(getContext()); - target.addLegalDialect(); - target.addIllegalOp(); + GreedyRewriteConfig rewriter_config = GreedyRewriteConfig(); + rewriter_config.enableRegionSimplification = + GreedySimplifyRegionLevel::Disabled; RewritePatternSet patterns(&getContext()); patterns.insert(&getContext()); - GreedyRewriteConfig rewriter_config = GreedyRewriteConfig(); if (failed(applyPatternsAndFoldGreedily(device, std::move(patterns), rewriter_config))) { signalPassFailure(); From 050453184f16de101e84224be9dba03aa9e63f6a Mon Sep 17 00:00:00 2001 From: andrej Date: Tue, 10 Sep 2024 13:36:21 -0600 Subject: [PATCH 2/4] add test --- .../blocks-with-args.mlir | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/bd-chains-and-dma-tasks/blocks-with-args.mlir diff --git a/test/bd-chains-and-dma-tasks/blocks-with-args.mlir b/test/bd-chains-and-dma-tasks/blocks-with-args.mlir new file mode 100644 index 0000000000..131b569dd5 --- /dev/null +++ b/test/bd-chains-and-dma-tasks/blocks-with-args.mlir @@ -0,0 +1,37 @@ +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// (c) Copyright 2024 AMD Inc. + +// RUN: aie-opt --aie-substitute-shim-dma-allocations %s + +// Region simplification lead to compiler crashes previously if blocks/regions +// had input arguments, such as created by scf.for loops with yield statements. +// This test ensures that the compiler does not crash with such IR present. + +module { + aie.device(npu1_4col) { + %tile_0_2 = aie.tile(0, 2) + %buf = aie.buffer(%tile_0_2) : memref<32xi32> + %buf0 = aie.buffer(%tile_0_2) : memref<32xi32> + %buf1 = aie.buffer(%tile_0_2) : memref<32xi32> + aie.core(%tile_0_2) { + %c0 = index.constant 0 + %c2 = index.constant 2 + %val_i32 = memref.load %buf[%c0] : memref<32xi32> + %val = index.castu %val_i32 : i32 to index + %val_mod_2 = index.rems %val, %c2 + %cond = index.cmp eq(%val_mod_2, %c0) + %res = scf.if %cond -> (memref<32xi32>) { + scf.yield %buf0 : memref<32xi32> + } else { + scf.yield %buf1 : memref<32xi32> + } + %res_load = memref.load %res[%c0] : memref<32xi32> + memref.store %res_load, %buf[%c0] : memref<32xi32> + aie.end + } + } +} From d6440c8565c2732d572f3d580b3b35ade8bc88ad Mon Sep 17 00:00:00 2001 From: andrej Date: Mon, 18 Nov 2024 17:40:48 -0700 Subject: [PATCH 3/4] add test --- .../conditional_buffer_selection.mlir | 88 +++++++++++++++++++ .../conditional_buffer_selection/run.lit | 6 ++ 2 files changed, 94 insertions(+) create mode 100644 test/npu-xrt/conditional_buffer_selection/conditional_buffer_selection.mlir create mode 100644 test/npu-xrt/conditional_buffer_selection/run.lit diff --git a/test/npu-xrt/conditional_buffer_selection/conditional_buffer_selection.mlir b/test/npu-xrt/conditional_buffer_selection/conditional_buffer_selection.mlir new file mode 100644 index 0000000000..1d6d766673 --- /dev/null +++ b/test/npu-xrt/conditional_buffer_selection/conditional_buffer_selection.mlir @@ -0,0 +1,88 @@ +//===- conditional_buffer_selection.mlir -----------------------*- MLIR -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// (c) Copyright 2024 Advanced Micro Devices, Inc. +// +//===----------------------------------------------------------------------===// +// +// The below lowered code was produced from the following higher level MLIR using the following pipeline: +// +// Pipeline: +// builtin.module(lower-affine,aie-canonicalize-device,aie.device(aie-assign-lock-ids,aie-register-objectFifos,aie-objectFifo-stateful-transform{ dynamic-objFifos=0 },aie-assign-bd-ids,aie-lower-cascade-flows,aie-lower-broadcast-packet,aie-lower-multicast,aie-assign-tile-controller-ids,aie-generate-column-control-overlay{ route-shim-to-tile-ctrl=0 },aie-assign-buffer-addresses{ alloc-scheme=bank-aware }),convert-scf-to-cf) +// +// MLIR: +// +// module { +// aie.device(npu1_4col) { +// +// func.func private @do_something(memref<64x32xf32>) +// +// %tile_0_2 = aie.tile(0, 2) +// %buf_a = aie.buffer(%tile_0_2) {sym_name = "buf_a"} : memref<64x32xf32> +// %buf_b = aie.buffer(%tile_0_2) {sym_name = "buf_b"} : memref<64x32xf32> +// +// %core_0_2 = aie.core(%tile_0_2) { +// +// %idx0 = index.constant 0 +// %idx1 = index.constant 1 +// +// %loop = scf.for %arg2 = %idx0 to %idx1 step %idx1 iter_args(%iter = %idx0) -> (index) { +// +// // Problematic section: we select a buffer based on a condition ... +// %cmp_res = index.cmp eq(%iter, %idx0) +// %branch_res = scf.if %cmp_res -> (memref<64x32xf32>) { +// scf.yield %buf_a : memref<64x32xf32> +// } else { +// scf.yield %buf_b : memref<64x32xf32> +// } +// // ... then use this buffer in some computation. +// func.call @do_something(%branch_res) : (memref<64x32xf32>) -> () +// +// %next_iter = index.add %idx0, %idx1 +// scf.yield %next_iter : index +// } +// +// aie.end +// } +// } +// } +// +// Test with: +// aie-opt --pass-pipeline="builtin.module(aie.device(aie-materialize-bd-chains,aie-substitute-shim-dma-allocations,aie-assign-runtime-sequence-bd-ids,aie-dma-tasks-to-npu,aie-dma-to-npu))" conditional_buffer_selection.mlir + +module { + aie.device(npu1_4col) { + %tile_0_0 = aie.tile(0, 0) + func.func private @do_something(memref<64x32xf32>) + %tile_0_2 = aie.tile(0, 2) {controller_id = #aie.packet_info} + %buf_a = aie.buffer(%tile_0_2) {address = 1024 : i32, mem_bank = 0 : i32, sym_name = "buf_a"} : memref<64x32xf32> + %buf_b = aie.buffer(%tile_0_2) {address = 16384 : i32, mem_bank = 1 : i32, sym_name = "buf_b"} : memref<64x32xf32> + %core_0_2 = aie.core(%tile_0_2) { + %idx0 = index.constant 0 + %idx1 = index.constant 1 + cf.br ^bb1(%idx0, %idx0 : index, index) + ^bb1(%0: index, %1: index): // 2 preds: ^bb0, ^bb6 + %2 = arith.cmpi slt, %0, %idx1 : index + cf.cond_br %2, ^bb2, ^bb7 + ^bb2: // pred: ^bb1 + %3 = index.cmp eq(%1, %idx0) + cf.cond_br %3, ^bb3, ^bb4 + ^bb3: // pred: ^bb2 + cf.br ^bb5(%buf_a : memref<64x32xf32>) + ^bb4: // pred: ^bb2 + cf.br ^bb5(%buf_b : memref<64x32xf32>) + ^bb5(%4: memref<64x32xf32>): // 2 preds: ^bb3, ^bb4 + cf.br ^bb6 + ^bb6: // pred: ^bb5 + func.call @do_something(%4) : (memref<64x32xf32>) -> () + %5 = index.add %idx0, %idx1 + %6 = arith.addi %0, %idx1 : index + cf.br ^bb1(%6, %5 : index, index) + ^bb7: // pred: ^bb1 + aie.end + } + } +} diff --git a/test/npu-xrt/conditional_buffer_selection/run.lit b/test/npu-xrt/conditional_buffer_selection/run.lit new file mode 100644 index 0000000000..12fa94e90a --- /dev/null +++ b/test/npu-xrt/conditional_buffer_selection/run.lit @@ -0,0 +1,6 @@ +// (c) Copyright 2024 Advanced Micro Devices, Inc. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// REQUIRES: +// +// RUN: aie-opt --pass-pipeline="builtin.module(aie.device(aie-materialize-bd-chains,aie-substitute-shim-dma-allocations,aie-assign-runtime-sequence-bd-ids,aie-dma-tasks-to-npu,aie-dma-to-npu))" %S/conditional_buffer_selection.mlir \ No newline at end of file From 670e20a718fffaf83bc5a583e2103af905c80cf0 Mon Sep 17 00:00:00 2001 From: andrej Date: Mon, 18 Nov 2024 17:42:43 -0700 Subject: [PATCH 4/4] remove old test that didn't test what I intended --- .../blocks-with-args.mlir | 37 ------------------- 1 file changed, 37 deletions(-) delete mode 100644 test/bd-chains-and-dma-tasks/blocks-with-args.mlir diff --git a/test/bd-chains-and-dma-tasks/blocks-with-args.mlir b/test/bd-chains-and-dma-tasks/blocks-with-args.mlir deleted file mode 100644 index 131b569dd5..0000000000 --- a/test/bd-chains-and-dma-tasks/blocks-with-args.mlir +++ /dev/null @@ -1,37 +0,0 @@ -// -// This file is licensed under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -// (c) Copyright 2024 AMD Inc. - -// RUN: aie-opt --aie-substitute-shim-dma-allocations %s - -// Region simplification lead to compiler crashes previously if blocks/regions -// had input arguments, such as created by scf.for loops with yield statements. -// This test ensures that the compiler does not crash with such IR present. - -module { - aie.device(npu1_4col) { - %tile_0_2 = aie.tile(0, 2) - %buf = aie.buffer(%tile_0_2) : memref<32xi32> - %buf0 = aie.buffer(%tile_0_2) : memref<32xi32> - %buf1 = aie.buffer(%tile_0_2) : memref<32xi32> - aie.core(%tile_0_2) { - %c0 = index.constant 0 - %c2 = index.constant 2 - %val_i32 = memref.load %buf[%c0] : memref<32xi32> - %val = index.castu %val_i32 : i32 to index - %val_mod_2 = index.rems %val, %c2 - %cond = index.cmp eq(%val_mod_2, %c0) - %res = scf.if %cond -> (memref<32xi32>) { - scf.yield %buf0 : memref<32xi32> - } else { - scf.yield %buf1 : memref<32xi32> - } - %res_load = memref.load %res[%c0] : memref<32xi32> - memref.store %res_load, %buf[%c0] : memref<32xi32> - aie.end - } - } -}