diff --git a/codegen/compiler/src/Quidditch/Conversion/CMakeLists.txt b/codegen/compiler/src/Quidditch/Conversion/CMakeLists.txt index f475b9d..a2b4853 100644 --- a/codegen/compiler/src/Quidditch/Conversion/CMakeLists.txt +++ b/codegen/compiler/src/Quidditch/Conversion/CMakeLists.txt @@ -46,6 +46,7 @@ iree_cc_library( "ConvertDMAToLLVM.cpp" DEPS Quidditch::Dialect::DMA::IR::DMADialect + Quidditch::Dialect::SnitchDMA::IR::SnitchDMADialect MLIRAnalysis MLIRIR MLIRLLVMCommonConversion diff --git a/codegen/compiler/src/Quidditch/Conversion/ConvertDMAToLLVM.cpp b/codegen/compiler/src/Quidditch/Conversion/ConvertDMAToLLVM.cpp index d29c81b..98430ad 100644 --- a/codegen/compiler/src/Quidditch/Conversion/ConvertDMAToLLVM.cpp +++ b/codegen/compiler/src/Quidditch/Conversion/ConvertDMAToLLVM.cpp @@ -9,8 +9,10 @@ #include "mlir/Transforms/DialectConversion.h" #include "Quidditch/Dialect/DMA/IR/DMAOps.h" +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.h" using namespace mlir; +using namespace quidditch; using namespace quidditch::dma; /// Returns the number of potentially non-contiguous outer dimensions of @@ -414,19 +416,7 @@ struct WaitForTransfersOpLowering : ConvertOpToLLVMPattern { rewriter.create(op->getLoc(), body); rewriter.setInsertionPointToEnd(body); - Value lastCompleted = - rewriter - .create( - op->getLoc(), /*res=*/rewriter.getI32Type(), - /*operands=*/ValueRange(), - // dmstati $0, 0 - // opcode6=0x2b, func3=0, func7=0b100, rd=$0, rs1=zero, - // rs2=imm5(0) - ".insn r 0x2b, 0, 0b100, $0, zero, zero\n", - /*constraints=*/"=r", - /*has_side_effects=*/true, /*is_align_stack=*/false, - /*asm_dialect=*/nullptr, /*operand_attrs=*/nullptr) - .getRes(); + Value lastCompleted = rewriter.create(op->getLoc()); Value notDone = rewriter.create( op->getLoc(), LLVM::ICmpPredicate::ult, lastCompleted, current); rewriter.create(op->getLoc(), notDone, body, after); @@ -450,6 +440,26 @@ struct CompletedTokenOpLowering : ConvertOpToLLVMPattern { } }; +struct StatOpLowering : ConvertOpToLLVMPattern { + using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; + + LogicalResult + matchAndRewrite(SnitchDMA::StatOp op, OpAdaptor adaptor, + ConversionPatternRewriter &rewriter) const override { + rewriter.replaceOpWithNewOp( + op, /*res=*/rewriter.getI32Type(), + /*operands=*/ValueRange(), + // dmstati $0, 0 + // opcode6=0x2b, func3=0, func7=0b100, rd=$0, rs1=zero, + // rs2=imm5(0) + ".insn r 0x2b, 0, 0b100, $0, zero, zero\n", + /*constraints=*/"=r", + /*has_side_effects=*/true, /*is_align_stack=*/false, + /*asm_dialect=*/nullptr, /*operand_attrs=*/nullptr); + return success(); + } +}; + } // namespace void quidditch::populateDMAToLLVMConversionPatterns( @@ -476,7 +486,8 @@ void quidditch::populateDMAToLLVMConversionPatterns( dmaStart2D->setAttr("hal.import.bitcode", builder.getUnitAttr()); patterns.insert(typeConverter); + StartZeroMemTransferOpOpLowering, StatOpLowering>( + typeConverter); patterns.insert(dmaStart1D, typeConverter); patterns.insert(dmaStart2D, typeConverter); patterns.insert( diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/CMakeLists.txt b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/CMakeLists.txt new file mode 100644 index 0000000..0e9c88b --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/CMakeLists.txt @@ -0,0 +1 @@ +iree_add_all_subdirs() diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/CMakeLists.txt b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/CMakeLists.txt new file mode 100644 index 0000000..c5e3b80 --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/CMakeLists.txt @@ -0,0 +1,73 @@ +iree_add_all_subdirs() + +iree_cc_library( + NAME + SnitchDMADialect + HDRS + "SnitchDMADialect.h" + "SnitchDMAOps.h" + TEXTUAL_HDRS + "SnitchDMAAttrs.cpp.inc" + "SnitchDMAAttrs.h.inc" + "SnitchDMADialect.cpp.inc" + "SnitchDMADialect.h.inc" + "SnitchDMAOps.cpp.inc" + "SnitchDMAOps.h.inc" + "SnitchDMATypes.cpp.inc" + "SnitchDMATypes.h.inc" + SRCS + "SnitchDMAAttrs.cpp" + "SnitchDMADialect.cpp" + "SnitchDMAOps.cpp" + "SnitchDMATypes.cpp" + DEPS + ::SnitchDMAAttrsGen + ::SnitchDMADialectGen + ::SnitchDMAOpsGen + ::SnitchDMATypesGen + LLVMSupport + MLIRIR + MLIRInferTypeOpInterface + MLIRSupport + PUBLIC +) + +iree_tablegen_library( + NAME + SnitchDMAOpsGen + TD_FILE + "SnitchDMAOps.td" + OUTS + --gen-op-decls SnitchDMAOps.h.inc + --gen-op-defs SnitchDMAOps.cpp.inc +) + +iree_tablegen_library( + NAME + SnitchDMADialectGen + TD_FILE + "SnitchDMADialect.td" + OUTS + --gen-dialect-decls SnitchDMADialect.h.inc + --gen-dialect-defs SnitchDMADialect.cpp.inc +) + +iree_tablegen_library( + NAME + SnitchDMAAttrsGen + TD_FILE + "SnitchDMAAttrs.td" + OUTS + --gen-attrdef-decls SnitchDMAAttrs.h.inc + --gen-attrdef-defs SnitchDMAAttrs.cpp.inc +) + +iree_tablegen_library( + NAME + SnitchDMATypesGen + TD_FILE + "SnitchDMATypes.td" + OUTS + --gen-typedef-decls SnitchDMATypes.h.inc + --gen-typedef-defs SnitchDMATypes.cpp.inc +) diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.cpp b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.cpp new file mode 100644 index 0000000..4305577 --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.cpp @@ -0,0 +1 @@ +#include "SnitchDMAAttrs.h" diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.h b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.h new file mode 100644 index 0000000..7592a83 --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.h @@ -0,0 +1,7 @@ + +#pragma once + +#include "mlir/IR/Attributes.h" + +#define GET_ATTRDEF_CLASSES +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.h.inc" diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.td b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.td new file mode 100644 index 0000000..1f7e45e --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.td @@ -0,0 +1,11 @@ +#ifndef QUIDDITCH_DIALECT_SNITCHDMA_IR_SNITCHDMAATTRS +#define QUIDDITCH_DIALECT_SNITCHDMA_IR_SNITCHDMAATTRS + +include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.td" +include "mlir/IR/AttrTypeBase.td" + +class SnitchDMA_Attr traits = []> : + AttrDef; + + +#endif diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.cpp b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.cpp new file mode 100644 index 0000000..3c7be86 --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.cpp @@ -0,0 +1,36 @@ +#include "SnitchDMADialect.h" + +#include "SnitchDMAAttrs.h" +#include "SnitchDMAOps.h" +#include "SnitchDMATypes.h" +#include "llvm/ADT/TypeSwitch.h" +#include "mlir/IR/DialectImplementation.h" +#include "mlir/IR/OpDefinition.h" +#include "mlir/IR/OpImplementation.h" + +#define GET_ATTRDEF_CLASSES +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.cpp.inc" + +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.cpp.inc" + +using namespace mlir; +using namespace quidditch::SnitchDMA; + +//===----------------------------------------------------------------------===// +// DMADialect +//===----------------------------------------------------------------------===// + +void SnitchDMADialect::initialize() { + addOperations< +#define GET_OP_LIST +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.cpp.inc" + >(); + addAttributes< +#define GET_ATTRDEF_LIST +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMAAttrs.cpp.inc" + >(); + addTypes< +#define GET_TYPEDEF_LIST +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.cpp.inc" + >(); +} diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.h b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.h new file mode 100644 index 0000000..f27eca6 --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.h @@ -0,0 +1,7 @@ + +#pragma once + +#include "mlir/IR/Dialect.h" +#include "mlir/IR/Operation.h" + +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.h.inc" diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.td b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.td new file mode 100644 index 0000000..e2c28d1 --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.td @@ -0,0 +1,21 @@ +#ifndef QUIDDITCH_DIALECT_SNITCHDMA_IR_SNITCHDMADIALECT +#define QUIDDITCH_DIALECT_SNITCHDMA_IR_SNITCHDMADIALECT + +include "mlir/IR/DialectBase.td" + +def SnitchDMA_Dialect : Dialect { + let name = "snitch_dma"; + let cppNamespace = "::quidditch::SnitchDMA"; + + let description = [{ + Dialect dealing with all implementation details specific to Snitch's DMA + engine. + Used to progressively lower and optimize the `dma` dialect. + }]; + + let useDefaultAttributePrinterParser = 0; + let useDefaultTypePrinterParser = 0; + let hasConstantMaterializer = 0; +} + +#endif diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.cpp b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.cpp new file mode 100644 index 0000000..d435087 --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.cpp @@ -0,0 +1,11 @@ +#include "SnitchDMAOps.h" + +#define GET_OP_CLASSES +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.cpp.inc" + +using namespace mlir; +using namespace quidditch::SnitchDMA; + +StringRef QueueResource::getName() { + return "queue"; +} diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.h b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.h new file mode 100644 index 0000000..6627f8a --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.h @@ -0,0 +1,20 @@ + +#pragma once + +#include "mlir/Bytecode/BytecodeOpInterface.h" +#include "mlir/IR/BuiltinAttributes.h" +#include "mlir/IR/OpImplementation.h" +#include "mlir/Interfaces/InferTypeOpInterface.h" +#include "mlir/Interfaces/SideEffectInterfaces.h" + +#include "SnitchDMATypes.h" + +#define GET_OP_CLASSES +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.h.inc" + +namespace quidditch::SnitchDMA { +class QueueResource : public mlir::SideEffects::Resource::Base { +public: + llvm::StringRef getName() override; +}; +} // namespace quidditch::SnitchDMA diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.td b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.td new file mode 100644 index 0000000..d89d7c4 --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMAOps.td @@ -0,0 +1,31 @@ +#ifndef QUIDDITCH_DIALECT_SNITCHDMA_IR_SNITCHDMAOPS +#define QUIDDITCH_DIALECT_SNITCHDMA_IR_SNITCHDMAOPS + +include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.td" +include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.td" +include "mlir/IR/CommonTypeConstraints.td" +include "mlir/IR/OpBase.td" +include "mlir/Interfaces/InferTypeOpInterface.td" +include "mlir/Interfaces/SideEffectInterfaces.td" + +class SnitchDMA_Op traits = []> : + Op; + +def SnitchDMA_QueueResource + : Resource<"quidditch::SnitchDMA::QueueResource">; + +def SnitchDMA_StatOp : SnitchDMA_Op<"stat", + [MemoryEffects<[MemRead]>]> { + + let description = [{ + Returns the id of the last DMA transfer that has been completed. + }]; + + let results = (outs I32:$completed_id); + + let assemblyFormat = [{ + attr-dict + }]; +} + +#endif diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.cpp b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.cpp new file mode 100644 index 0000000..7cfbb96 --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.cpp @@ -0,0 +1,11 @@ +#include "SnitchDMATypes.h" + +#include "llvm/ADT/TypeSwitch.h" +#include "mlir/IR/DialectImplementation.h" +#include "mlir/IR/OpDefinition.h" +#include "mlir/IR/OpImplementation.h" + +#include "SnitchDMADialect.h" + +#define GET_TYPEDEF_CLASSES +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.cpp.inc" diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.h b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.h new file mode 100644 index 0000000..427b5fb --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.h @@ -0,0 +1,7 @@ + +#pragma once + +#include "mlir/IR/Types.h" + +#define GET_TYPEDEF_CLASSES +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.h.inc" diff --git a/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.td b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.td new file mode 100644 index 0000000..b9e1189 --- /dev/null +++ b/codegen/compiler/src/Quidditch/Dialect/SnitchDMA/IR/SnitchDMATypes.td @@ -0,0 +1,10 @@ +#ifndef QUIDDITCH_DIALECT_SNITCHDMA_IR_SNITCHDMATYPES +#define QUIDDITCH_DIALECT_SNITCHDMA_IR_SNITCHDMATYPES + +include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.td" +include "mlir/IR/AttrTypeBase.td" + +class SnitchDMA_Type traits = []> : + TypeDef; + +#endif diff --git a/codegen/compiler/src/Quidditch/Target/ConvertToLLVM.cpp b/codegen/compiler/src/Quidditch/Target/ConvertToLLVM.cpp index 3a4138e..dd73b6e 100644 --- a/codegen/compiler/src/Quidditch/Target/ConvertToLLVM.cpp +++ b/codegen/compiler/src/Quidditch/Target/ConvertToLLVM.cpp @@ -7,6 +7,7 @@ #include "Quidditch/Conversion/ConvertDMAToLLVM.h" #include "Quidditch/Conversion/ConvertSnitchToLLVM.h" #include "Quidditch/Dialect/Snitch/IR/QuidditchSnitchDialect.h" +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.h" #include "iree/compiler/Codegen/LLVMCPU/DispatchABI.h" #include "iree/compiler/Codegen/LLVMCPU/PassDetail.h" #include "iree/compiler/Codegen/LLVMCPU/Passes.h" diff --git a/codegen/compiler/src/Quidditch/Target/Passes.td b/codegen/compiler/src/Quidditch/Target/Passes.td index d954a07..bb6e7aa 100644 --- a/codegen/compiler/src/Quidditch/Target/Passes.td +++ b/codegen/compiler/src/Quidditch/Target/Passes.td @@ -56,6 +56,7 @@ def ConvertToLLVMPass : Pass<"quidditch-convert-to-llvm", "mlir::ModuleOp"> { "mlir::scf::SCFDialect", "mlir::memref::MemRefDialect", "mlir::affine::AffineDialect", + "quidditch::SnitchDMA::SnitchDMADialect", ]; } diff --git a/codegen/compiler/src/Quidditch/Target/QuidditchTarget.cpp b/codegen/compiler/src/Quidditch/Target/QuidditchTarget.cpp index dc51630..e49242d 100644 --- a/codegen/compiler/src/Quidditch/Target/QuidditchTarget.cpp +++ b/codegen/compiler/src/Quidditch/Target/QuidditchTarget.cpp @@ -31,6 +31,7 @@ #include "Quidditch/Dialect/Snitch/IR/QuidditchSnitchDialect.h" #include "Quidditch/Dialect/Snitch/IR/QuidditchSnitchOps.h" #include "Quidditch/Dialect/Snitch/Transforms/Passes.h" +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.h" #include "compiler/plugins/target/LLVMCPU/LinkerTool.h" #include "compiler/plugins/target/LLVMCPU/StaticLibraryGenerator.h" @@ -136,7 +137,8 @@ class QuidditchTargetBackend final : public IREE::HAL::TargetBackend { registry.insert(); + quidditch::dma::DMADialect, + quidditch::SnitchDMA::SnitchDMADialect>(); } void getDefaultExecutableTargets( diff --git a/codegen/tools/CMakeLists.txt b/codegen/tools/CMakeLists.txt index e3da2fb..910c010 100644 --- a/codegen/tools/CMakeLists.txt +++ b/codegen/tools/CMakeLists.txt @@ -5,6 +5,7 @@ target_link_libraries(quidditch-opt Quidditch::Conversion::ConvertSnitchToLLVM Quidditch::Conversion::ConvertToRISCV Quidditch::Dialect::DMA::Extensions::DMACoreSpecializationOpInterfaceImpl + Quidditch::Dialect::SnitchDMA::IR::SnitchDMADialect Quidditch::Dialect::Snitch::IR::QuidditchSnitchDialect Quidditch::Dialect::Snitch::Transforms::Passes Quidditch::Target::Passes diff --git a/codegen/tools/quidditch-opt.cpp b/codegen/tools/quidditch-opt.cpp index 68cd9e9..5f826db 100644 --- a/codegen/tools/quidditch-opt.cpp +++ b/codegen/tools/quidditch-opt.cpp @@ -4,6 +4,7 @@ #include "Quidditch/Conversion/Passes.h" #include "Quidditch/Dialect/DMA/Extensions/DMACoreSpecializationOpInterfaceImpl.h" #include "Quidditch/Dialect/DMA/IR/DMADialect.h" +#include "Quidditch/Dialect/SnitchDMA/IR/SnitchDMADialect.h" #include "Quidditch/Dialect/Snitch/IR/QuidditchSnitchDialect.h" #include "Quidditch/Dialect/Snitch/Transforms/Passes.h" #include "Quidditch/Target/Passes.h" @@ -31,7 +32,8 @@ int main(int argc, char **argv) { quidditch::dma::registerDMACoreSpecializationOpInterface(registry); iree_compiler::registerAllDialects(registry); registry.insert(); + quidditch::dma::DMADialect, + quidditch::SnitchDMA::SnitchDMADialect>(); quidditch::registerPasses(); quidditch::registerConversionPasses();