| 21 | |
| 22 | |
| 23 | NB_MODULE(_cuda_tile, m) { |
| 24 | //===--------------------------------------------------------------------===// |
| 25 | // CudaTile dialect/pass registration |
| 26 | //===--------------------------------------------------------------------===// |
| 27 | m.def( |
| 28 | "register_dialect", |
| 29 | [](MlirContext context, bool load) { |
| 30 | MlirDialectHandle handle = mlirGetDialectHandle__cuda_tile__(); |
| 31 | mlirDialectHandleRegisterDialect(handle, context); |
| 32 | if (load) { |
| 33 | mlirDialectHandleLoadDialect(handle, context); |
| 34 | } |
| 35 | }, |
| 36 | nb::arg("context") = nb::none(), nb::arg("load") = true); |
| 37 | |
| 38 | m.def("register_passes", []() { mlirCudaTileRegisterPasses(); }); |
| 39 | |
| 40 | // Create a simple struct to avoid C++ symbol binding issues with |
| 41 | // EMBED_CAPI_LINK_LIBS |
| 42 | struct TileIROptimizationsOptsWrapper { |
| 43 | int opt_level = 3; |
| 44 | bool fuse_fma = false; |
| 45 | }; |
| 46 | |
| 47 | nb::class_<TileIROptimizationsOptsWrapper>(m, "TileIROptimizationsOpts") |
| 48 | .def(nb::init<>()) |
| 49 | .def_rw("opt_level", &TileIROptimizationsOptsWrapper::opt_level) |
| 50 | .def_rw("fuse_fma", &TileIROptimizationsOptsWrapper::fuse_fma); |
| 51 | |
| 52 | // TODO: Add CudaTile python bindings tests for ir passes |
| 53 | m.def( |
| 54 | "applyTileIROptimizations", |
| 55 | [](nb::object &moduleOp, const TileIROptimizationsOptsWrapper &opts) { |
| 56 | mlirCudaTileOptConfig config; |
| 57 | mlirCudaTileOptFlagsInit(&config); |
| 58 | config.optLevel = opts.opt_level; |
| 59 | if (opts.fuse_fma) |
| 60 | config.flags |= CUDATILE_OPT_FLAG_FUSE_FMA; |
| 61 | MlirOperation mlirOp = nb::cast<MlirOperation>(moduleOp); |
| 62 | return mlirLogicalResultIsSuccess( |
| 63 | mlirCudaTileApplyOptimizations(mlirOp, &config)); |
| 64 | }, |
| 65 | nb::arg("module"), nb::arg("opts") = TileIROptimizationsOptsWrapper{}, |
| 66 | "Perform CUDA Tile IR optimizations using CAPI wrapper"); |
| 67 | |
| 68 | m.def( |
| 69 | "addLoopSplitThresholdAttr", |
| 70 | [](nb::object &Op, const int threshold) { |
| 71 | MlirOperation mlirOp = nb::cast<MlirOperation>(Op); |
| 72 | MlirContext ctx = mlirOperationGetContext(mlirOp); |
| 73 | MlirType i32Type = mlirCudaTileIntegerTypeGet(ctx, 32); |
| 74 | MlirAttribute thresholdAttr = |
| 75 | mlirCudaTileIntegerAttrGet(i32Type, threshold); |
| 76 | MlirStringRef attrName = |
| 77 | mlirStringRefCreateFromCString("loop_split_threshold"); |
| 78 | mlirCudaTileOperationSetDiscardableAttributeByName(mlirOp, attrName, |
| 79 | thresholdAttr); |
| 80 | }, |
nothing calls this directly
no test coverage detected