Skip to content
Closed
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
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ cmake_minimum_required(VERSION 3.13.4)
include(CheckCXXSourceCompiles)

set(POLYGEIST_ENABLE_CUDA 0 CACHE BOOL "Enable CUDA frontend and backend")
set(POLYGEIST_ENABLE_CUDA_SYNTAX_ONLY 0 CACHE BOOL "Enable CUDA syntax parsing without requiring CUDA toolkit")
set(POLYGEIST_ENABLE_ROCM 0 CACHE BOOL "Enable ROCM backend")

# If CUDA_SYNTAX_ONLY is enabled, set flag for frontend-only mode
if(POLYGEIST_ENABLE_CUDA_SYNTAX_ONLY)
set(POLYGEIST_CUDA_FRONTEND_ONLY 1)
message(STATUS "CUDA syntax-only mode enabled (no CUDA toolkit required)")
endif()

set(POLYGEIST_ENABLE_POLYMER 0 CACHE BOOL "Enable Polymer")
set(POLYGEIST_POLYMER_ENABLE_ISL 0 CACHE BOOL "Enable Polymer isl")
set(POLYGEIST_POLYMER_ENABLE_PLUTO 0 CACHE BOOL "Enable Polymer pluto")
Expand Down
3 changes: 3 additions & 0 deletions include/polygeist/Passes/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ createGpuSerializeToHsacoPass(StringRef arch, StringRef features,
void registerGpuSerializeToCubinPass();
void registerGpuSerializeToHsacoPass();

std::unique_ptr<Pass> createConvertGPUToVortexPass();
std::unique_ptr<Pass> createGenerateVortexMainPass();

void populateForBreakToWhilePatterns(RewritePatternSet &patterns);
} // namespace polygeist
} // namespace mlir
Expand Down
48 changes: 48 additions & 0 deletions include/polygeist/Passes/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,52 @@ def ConvertPolygeistToLLVM : Pass<"convert-polygeist-to-llvm", "mlir::ModuleOp">
];
}

def ConvertGPUToVortex : Pass<"convert-gpu-to-vortex", "ModuleOp"> {
let summary = "Lower GPU dialect operations to Vortex RISC-V intrinsics";
let description = [{
This pass converts GPU dialect operations to LLVM dialect with Vortex-specific
intrinsics. It lowers operations like gpu.thread_id, gpu.block_id to RISC-V
CSR reads via inline assembly, preparing the code for the Vortex GPGPU backend.

Example:
%tid = gpu.thread_id x
becomes:
%tid = llvm.inline_asm "csrr $0, 0xCC0" : () -> i32

The pass automatically emits JSON metadata files describing kernel argument
layouts for runtime argument marshaling. Files are written to the current
working directory as <kernel_name>.meta.json.
}];
let constructor = "mlir::polygeist::createConvertGPUToVortexPass()";
let dependentDialects = [
"LLVM::LLVMDialect",
"gpu::GPUDialect",
];
}

def GenerateVortexMain : Pass<"generate-vortex-main", "ModuleOp"> {
let summary = "Generate Vortex main() wrapper for kernel execution";
let description = [{
This pass generates the Vortex-specific main() entry point and kernel_body
wrapper function. It should run AFTER gpu-to-llvm lowering has converted
gpu.func to llvm.func.

The generated code:
1. main() function that:
- Reads kernel args from VX_CSR_MSCRATCH (0x340) via inline assembly
- Calls vx_spawn_threads() with grid dimensions and kernel callback
2. kernel_body() wrapper that:
- Takes void* args pointer
- Unpacks individual arguments from the struct
- Calls the original lowered kernel function

This matches the Vortex kernel execution model where kernels are launched
via vx_spawn_threads() with a callback function.
}];
let constructor = "mlir::polygeist::createGenerateVortexMainPass()";
let dependentDialects = [
"LLVM::LLVMDialect",
];
}

#endif // POLYGEIST_PASSES
6 changes: 6 additions & 0 deletions lib/polygeist/ExecutionEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# TODO we do not support cross compilation currently

# Skip execution engine entirely if syntax-only mode
if(POLYGEIST_CUDA_FRONTEND_ONLY)
message(STATUS "Skipping CUDA execution engine (syntax-only mode)")
return()
endif()

if(POLYGEIST_ENABLE_CUDA)
find_package(CUDA)
enable_language(CUDA)
Expand Down
5 changes: 4 additions & 1 deletion lib/polygeist/Passes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ add_mlir_dialect_library(MLIRPolygeistTransforms
InnerSerialization.cpp
ForBreakToWhile.cpp
ConvertParallelToGPU.cpp
ConvertGPUToVortex.cpp
GenerateVortexMain.cpp
SerializeToCubin.cpp
SerializeToHsaco.cpp
ParallelLoopUnroll.cpp
Expand Down Expand Up @@ -80,7 +82,8 @@ target_compile_definitions(obj.MLIRPolygeistTransforms
POLYGEIST_PGO_DATA_DIR_ENV_VAR="${POLYGEIST_PGO_DATA_DIR_ENV_VAR}"
)

if(POLYGEIST_ENABLE_CUDA)
# Only require CUDA toolkit if full CUDA support (not syntax-only mode)
if(POLYGEIST_ENABLE_CUDA AND NOT POLYGEIST_CUDA_FRONTEND_ONLY)
find_package(CUDA)
enable_language(CUDA)

Expand Down
Loading
Loading