Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't compile chess wrappers for every test. #1494

Merged
merged 3 commits into from
May 17, 2024
Merged
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
19 changes: 17 additions & 2 deletions aie_runtime_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,25 @@ function(add_aie_runtime_libs arch)
add_dependencies(aie-runtime-libs ${arch}_me_basic)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/me_basic.o DESTINATION ${CMAKE_INSTALL_PREFIX}/aie_runtime_lib/${arch})


# Precompile the intrinsic wrappers.
if(DEFINED VITIS_ROOT)
add_custom_target(${arch}_chess_intrinsic_wrapper ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/chess_intrinsic_wrapper.ll)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/chess_intrinsic_wrapper.ll
COMMAND ${VITIS_XCHESSCC} -p me -P ${VITIS_${arch}_INCLUDE_DIR}
-C Release_LLVM
-I ${VITIS_${arch}_INCLUDE_DIR}/runtime/include/
-d -c ${CMAKE_CURRENT_SOURCE_DIR}/chess_intrinsic_wrapper.cpp
-f +f
-o ${CMAKE_CURRENT_BINARY_DIR}/chess_intrinsic_wrapper.ll
COMMAND sed -i s/^target.*//
${CMAKE_CURRENT_BINARY_DIR}/chess_intrinsic_wrapper.ll
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/chess_intrinsic_wrapper.cpp)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/chess_intrinsic_wrapper.ll DESTINATION ${CMAKE_INSTALL_PREFIX}/aie_runtime_lib/${arch})
add_dependencies(aie-runtime-libs ${arch}_chess_intrinsic_wrapper)
endif()

set(INSTALLS
chess_intrinsic_wrapper.cpp
lut_based_ops.cpp
lut_based_ops.h
vec_math.h)
Expand Down
52 changes: 11 additions & 41 deletions python/compiler/aiecc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def aie_target_defines(aie_target):
return ["-D__AIEARCH__=10"]


def chesshack(llvmir_chesslinked):
def downgrade_ir_for_chess(llvmir_chesslinked):
llvmir_chesslinked = (
llvmir_chesslinked.replace("memory(none)", "readnone")
.replace("memory(read)", "readonly")
Expand Down Expand Up @@ -392,13 +392,19 @@ async def do_call(self, task, command, force=False):
sys.exit(ret)

# In order to run xchesscc on modern ll code, we need a bunch of hacks.
async def chesshack(self, task, llvmir, chess_intrinsic_wrapper_ll_path):
async def chesshack(self, task, llvmir, aie_target):
llvmir_chesshack = llvmir + "chesshack.ll"
llvmir_chesslinked_path = llvmir + "chesslinked.ll"
if not self.opts.execute:
return llvmir_chesslinked_path
llvmir = await read_file_async(llvmir)

install_path = aie.compiler.aiecc.configure.install_path()
runtime_lib_path = os.path.join(install_path, "aie_runtime_lib")
chess_intrinsic_wrapper_ll_path = os.path.join(
runtime_lib_path, aie_target.upper(), "chess_intrinsic_wrapper.ll"
)

await write_file_async(llvmir, llvmir_chesshack)
assert os.path.exists(llvmir_chesshack)
await self.do_call(
Expand All @@ -414,47 +420,16 @@ async def chesshack(self, task, llvmir, chess_intrinsic_wrapper_ll_path):
)

llvmir_chesslinked_ir = await read_file_async(llvmir_chesslinked_path)
llvmir_chesslinked_ir = chesshack(llvmir_chesslinked_ir)
llvmir_chesslinked_ir = downgrade_ir_for_chess(llvmir_chesslinked_ir)
await write_file_async(llvmir_chesslinked_ir, llvmir_chesslinked_path)

return llvmir_chesslinked_path

async def prepare_for_chesshack(self, task, aie_target):
if opts.compile and opts.xchesscc:
install_path = aie.compiler.aiecc.configure.install_path()
runtime_lib_path = os.path.join(install_path, "aie_runtime_lib")
chess_intrinsic_wrapper_cpp = os.path.join(
runtime_lib_path, aie_target.upper(), "chess_intrinsic_wrapper.cpp"
)

chess_intrinsic_wrapper_ll_path = self.prepend_tmp(
"chess_intrinsic_wrapper.ll"
)

# fmt: off
await self.do_call(task, ["xchesscc_wrapper", aie_target.lower(), "+w", self.prepend_tmp("work"), "-c", "-d", "-f", "+f", "+P", "4", chess_intrinsic_wrapper_cpp, "-o", chess_intrinsic_wrapper_ll_path])
# fmt: on

# this has to be here and not higher because there are tests that check for the command string for the above do_call
if not self.opts.execute:
return
chess_intrinsic_wrapper = await read_file_async(
chess_intrinsic_wrapper_ll_path
)
chess_intrinsic_wrapper = re.sub(
r"^target.*", "", chess_intrinsic_wrapper, flags=re.MULTILINE
)
await write_file_async(
chess_intrinsic_wrapper, chess_intrinsic_wrapper_ll_path
)
return chess_intrinsic_wrapper_ll_path

async def process_core(
self,
core,
aie_target,
aie_peano_target,
chess_intrinsic_wrapper_ll_path,
file_with_addresses,
):
async with self.limit:
Expand Down Expand Up @@ -522,7 +497,7 @@ async def process_core(

if opts.compile and opts.xchesscc:
if not opts.unified:
file_core_llvmir_chesslinked = await self.chesshack(task, file_core_llvmir, chess_intrinsic_wrapper_ll_path)
file_core_llvmir_chesslinked = await self.chesshack(task, file_core_llvmir, aie_target)
if self.opts.link and self.opts.xbridge:
link_with_obj = await extract_input_files(file_core_bcf)
await self.do_call(task, ["xchesscc_wrapper", aie_target.lower(), "+w", self.prepend_tmp("work"), "-d", "-f", "+P", "4", file_core_llvmir_chesslinked, link_with_obj, "+l", file_core_bcf, "-o", file_core_elf])
Expand Down Expand Up @@ -1053,10 +1028,6 @@ async def run_flow(self):
if opts.only_npu:
return

chess_intrinsic_wrapper_ll_path = await self.prepare_for_chesshack(
progress_bar.task, aie_target
)

# fmt: off
if opts.unified:
file_opt_with_addresses = self.prepend_tmp("input_opt_with_addresses.mlir")
Expand All @@ -1067,7 +1038,7 @@ async def run_flow(self):

self.unified_file_core_obj = self.prepend_tmp("input.o")
if opts.compile and opts.xchesscc:
file_llvmir_hacked = await self.chesshack(progress_bar.task, file_llvmir, chess_intrinsic_wrapper_ll_path)
file_llvmir_hacked = await self.chesshack(progress_bar.task, file_llvmir, aie_target)
await self.do_call(progress_bar.task, ["xchesscc_wrapper", aie_target.lower(), "+w", self.prepend_tmp("work"), "-c", "-d", "-f", "+P", "4", file_llvmir_hacked, "-o", self.unified_file_core_obj])
elif opts.compile:
file_llvmir_opt = self.prepend_tmp("input.opt.ll")
Expand Down Expand Up @@ -1095,7 +1066,6 @@ async def run_flow(self):
core,
aie_target,
aie_peano_target,
chess_intrinsic_wrapper_ll_path,
file_with_addresses,
)
)
Expand Down
104 changes: 60 additions & 44 deletions test/aie2xclbin/buffers_xclbin.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,109 @@

// REQUIRES: peano

// RUN: aie2xclbin -v --host-target=aarch64-linux-gnu --peano=%PEANO_INSTALL_DIR %s --xclbin-name=test.xclbin

// RUN: FileCheck %s --input-file=buffers_xclbin.mlir.prj/kernels.json
// RUN: aie2xclbin -v --host-target=aarch64-linux-gnu --peano=%PEANO_INSTALL_DIR %s --tmpdir=%T/buffers_xclbin.mlir.prj --xclbin-name=test.xclbin
// RUN: FileCheck %s --input-file=%T/buffers_xclbin.mlir.prj/kernels.json

// CHECK: {
// CHECK: "ps-kernels": {
// CHECK: "kernels": [
// CHECK: {
// CHECK: "name": "MLIR_AIE",
// CHECK: "type": "dpu",
// CHECK: "extended-data": {
// CHECK: "subtype": "DPU",
// CHECK: "functional": "1",
// CHECK: "dpu_kernel_id": "0x901"
// CHECK: },
// CHECK: "arguments": [
// CHECK: {
// CHECK: "name": "instr",
// CHECK: "memory-connection": "SRAM",
// CHECK: "address-qualifier": "GLOBAL",
// CHECK: "type": "char *",
// CHECK: "offset": "0x00"
// CHECK: "memory-connection": "SRAM",
// CHECK: "name": "instr",
// CHECK: "offset": "0x00",
// CHECK: "type": "char *"
// CHECK: },
// CHECK: {
// CHECK: "name": "ninstr",
// CHECK: "address-qualifier": "SCALAR",
// CHECK: "type": "uint64_t",
// CHECK: "offset": "0x08"
// CHECK: "name": "ninstr",
// CHECK: "offset": "0x08",
// CHECK: "type": "uint64_t"
// CHECK: },
// CHECK: {
// CHECK: "name": "bo0",
// CHECK: "memory-connection": "HOST",
// CHECK: "address-qualifier": "GLOBAL",
// CHECK: "type": "char *",
// CHECK: "offset": "0x10"
// CHECK: "memory-connection": "HOST",
// CHECK: "name": "bo0",
// CHECK: "offset": "0x10",
// CHECK: "type": "char *"
// CHECK: },
// CHECK: {
// CHECK: "name": "bo1",
// CHECK: "memory-connection": "HOST",
// CHECK: "address-qualifier": "GLOBAL",
// CHECK: "type": "char *",
// CHECK: "offset": "0x18"
// CHECK: "memory-connection": "HOST",
// CHECK: "name": "bo1",
// CHECK: "offset": "0x18",
// CHECK: "type": "char *"
// CHECK: },
// CHECK: {
// CHECK: "name": "bo2",
// CHECK: "memory-connection": "HOST",
// CHECK: "address-qualifier": "GLOBAL",
// CHECK: "type": "char *",
// CHECK: "offset": "0x20"
// CHECK: "memory-connection": "HOST",
// CHECK: "name": "bo2",
// CHECK: "offset": "0x20",
// CHECK: "type": "char *"
// CHECK: },
// CHECK: {
// CHECK: "name": "bo3",
// CHECK: "memory-connection": "HOST",
// CHECK: "address-qualifier": "GLOBAL",
// CHECK: "type": "char *",
// CHECK: "offset": "0x28"
// CHECK: "memory-connection": "HOST",
// CHECK: "name": "bo3",
// CHECK: "offset": "0x28",
// CHECK: "type": "char *"
// CHECK: },
// CHECK: {
// CHECK: "name": "bo4",
// CHECK: "memory-connection": "HOST",
// CHECK: "address-qualifier": "GLOBAL",
// CHECK: "type": "char *",
// CHECK: "offset": "0x30"
// CHECK: "memory-connection": "HOST",
// CHECK: "name": "bo4",
// CHECK: "offset": "0x30",
// CHECK: "type": "char *"
// CHECK: },
// CHECK: {
// CHECK: "name": "bo5",
// CHECK: "memory-connection": "HOST",
// CHECK: "address-qualifier": "GLOBAL",
// CHECK: "type": "char *",
// CHECK: "offset": "0x38"
// CHECK: "memory-connection": "HOST",
// CHECK: "name": "bo5",
// CHECK: "offset": "0x38",
// CHECK: "type": "char *"
// CHECK: }
// CHECK: ],
// CHECK: "extended-data": {
// CHECK: "dpu_kernel_id": "0x901",
// CHECK: "functional": "1",
// CHECK: "subtype": "DPU"
// CHECK: },
// CHECK: "instances": [
// CHECK: {
// CHECK: "name": "MLIRAIE"
// CHECK: "name": "MLIRAIEV1"
// CHECK: }
// CHECK: ]
// CHECK: ],
// CHECK: "name": "MLIR_AIE",
// CHECK: "type": "dpu"
// CHECK: }
// CHECK: ]
// CHECK: }
// CHECK: }

module {
aie.device(npu1) {
aie.device(npu1_4col) {
memref.global "public" @in0 : memref<1024xi32>
memref.global "public" @out0 : memref<1024xi32>
memref.global "public" @in1 : memref<1024xi32>
memref.global "public" @out1 : memref<1024xi32>
memref.global "public" @in2 : memref<1024xi32>
memref.global "public" @out2 : memref<1024xi32>
%02 = aie.tile(0, 2)
%12 = aie.tile(1, 2)
%22 = aie.tile(2, 2)

aie.core(%12) {
aie.end
}
aie.shim_dma_allocation @in0(MM2S, 0, 0)
aie.shim_dma_allocation @out0(S2MM, 0, 0)
aie.shim_dma_allocation @in1(MM2S, 1, 0)
aie.shim_dma_allocation @out1(S2MM, 1, 0)
aie.shim_dma_allocation @in2(MM2S, 2, 0)
aie.shim_dma_allocation @out2(S2MM, 2, 0)

func.func @sequence(%arg0: memref<1024xi32>, %arg1: memref<1024xi32>, %arg2: memref<1024xi32>, %arg3: memref<1024xi32>, %arg4: memref<1024xi32>, %arg5: memref<1024xi32>) {
aiex.npu.dma_memcpy_nd(0, 0, %arg0[0, 0, 0, 0][1, 1, 1, 1024][0, 0, 0]) {id = 0 : i64, metadata = @in0} : memref<1024xi32>
aiex.npu.dma_memcpy_nd(0, 0, %arg1[0, 0, 0, 0][1, 1, 1, 1024][0, 0, 0]) {id = 1 : i64, metadata = @out0} : memref<1024xi32>
Expand Down
32 changes: 32 additions & 0 deletions test/aie2xclbin/simple_xclbin_chess.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===- simple.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 2023 Xilinx Inc.
//
//===----------------------------------------------------------------------===//

// RUN: aie2xclbin -v --use-chess --host-target=aarch64-linux-gnu %s --xclbin-name=test.xclbin | FileCheck %s
// REQUIRES: valid_xchess_license

// Note that llc determines the architecture from the llvm IR.
// CHECK-NOT: llc
// CHECK: xchesscc_wrapper
// CHECK: bootgen
// CHECK: xclbinutil
// CHECK-NOT: llc

module {
aie.device(npu1_4col) {
%12 = aie.tile(1, 2)
%buf = aie.buffer(%12) : memref<256xi32>
%4 = aie.core(%12) {
%0 = arith.constant 0 : i32
%1 = arith.constant 0 : index
memref.store %0, %buf[%1] : memref<256xi32>
aie.end
}
}
}
3 changes: 2 additions & 1 deletion test/aiecc/simple.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
// RUN: %PYTHON aiecc.py --compile --xchesscc --no-link -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=XCHESSCC
// RUN: %PYTHON aiecc.py --compile --no-xchesscc --no-link -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=PEANO
// RUN: %PYTHON aiecc.py --no-compile --no-link -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=NOCOMPILE
// RUN: %PYTHON aiecc.py --no-unified --compile --no-link --xchesscc -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=XCHESSCC
// As an optimization, this case compiles and links together, so --no-link results in no compilation either.
// RUN: %PYTHON aiecc.py --no-unified --compile --no-link --xchesscc -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=NOCOMPILE
// RUN: %PYTHON aiecc.py --no-unified --compile --no-link --no-xchesscc -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=PEANO
// RUN: %PYTHON aiecc.py --no-unified --no-compile --no-link -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=NOCOMPILE

Expand Down
3 changes: 2 additions & 1 deletion test/aiecc/simple_aie2.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
// RUN: %PYTHON aiecc.py --compile --xchesscc --no-link -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=XCHESSCC
// RUN: %PYTHON aiecc.py --compile --no-xchesscc --no-link -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=PEANO
// RUN: %PYTHON aiecc.py --no-compile --no-link -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=NOCOMPILE
// RUN: %PYTHON aiecc.py --no-unified --compile --no-link --xchesscc -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=XCHESSCC
// As an optimization, this case compiles and links together, so --no-link results in no compilation either.
// RUN: %PYTHON aiecc.py --no-unified --compile --no-link --xchesscc -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=NOCOMPILE
// RUN: %PYTHON aiecc.py --no-unified --compile --no-link --no-xchesscc -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=PEANO
// RUN: %PYTHON aiecc.py --no-unified --no-compile --no-link -nv %VitisSysrootFlag% --host-target=%aieHostTargetTriplet% %s -I%aie_runtime_lib%/test_lib/include %extraAieCcFlags% -L%aie_runtime_lib%/test_lib/lib -ltest_lib %S/test.cpp -o test.elf | FileCheck %s --check-prefix=NOCOMPILE

Expand Down
38 changes: 3 additions & 35 deletions tools/aie2xclbin/XCLBinGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,41 +715,9 @@ static LogicalResult generateUnifiedObject(MLIRContext *context,
SmallString<64> chessworkDir(TK.TempDir);
sys::path::append(chessworkDir, "chesswork");

SmallString<64> chessIntrinsicsCpp(TK.InstallDir);
sys::path::append(chessIntrinsicsCpp, "aie_runtime_lib", TK.TargetArch,
"chess_intrinsic_wrapper.cpp");

SmallString<64> chessIntrinsicsLL(TK.TempDir);
sys::path::append(chessIntrinsicsLL, "chess_intrinsic_wrapper.ll");

if (runTool(chessWrapperBin,
{StringRef(TK.TargetArch).lower(), "+w",
std::string(chessworkDir), "-c", "-d", "-f", "+f", "+P", "4",
std::string(chessIntrinsicsCpp), "-o",
std::string(chessIntrinsicsLL)},
TK.Verbose) != 0)
return moduleOp.emitOpError("Failed to compile chess intrinsics");

std::string newIntrinsicsLL;
{
auto chessIntrinsicIn = openInputFile(chessIntrinsicsLL, &errorMessage);
if (!chessIntrinsicIn)
moduleOp.emitOpError(errorMessage);

newIntrinsicsLL =
std::regex_replace(std::string(chessIntrinsicIn->getBuffer()),
std::regex("target datalayout.*"), "");
newIntrinsicsLL = std::regex_replace(newIntrinsicsLL,
std::regex("target triple.*"), "");
}
{
auto chessIntrinsicOut = openOutputFile(chessIntrinsicsLL);
if (!chessIntrinsicOut)
moduleOp.emitOpError(errorMessage);

chessIntrinsicOut->os() << newIntrinsicsLL;
chessIntrinsicOut->keep();
}
SmallString<64> chessIntrinsicsLL(TK.InstallDir);
sys::path::append(chessIntrinsicsLL, "aie_runtime_lib", TK.TargetArch,
"chess_intrinsic_wrapper.ll");

std::string llvmirString;
{
Expand Down
Loading