Skip to content

Commit

Permalink
[DMA] Split DMA operations into its own dialect
Browse files Browse the repository at this point in the history
`quidditch_snitch` has turned into a bit of a god-dialect which even contains things not specific to snitch.
This PR is the first step in reducing this by splitting everything DMA related out of the dialect.
Future goal is to have lowerings of just DMA operations to lower-level hardware specific dialects where hardware specific optimizations and legalizations can occur.
  • Loading branch information
zero9178 committed Aug 31, 2024
1 parent 7466aa6 commit 36ae19f
Show file tree
Hide file tree
Showing 51 changed files with 1,849 additions and 1,465 deletions.
15 changes: 15 additions & 0 deletions codegen/compiler/src/Quidditch/Conversion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ iree_cc_library(
MLIRSCFDialect
MLIRTransforms
)

iree_cc_library(
NAME
ConvertDMAToLLVM
SRCS
"ConvertDMAToLLVM.cpp"
DEPS
Quidditch::Dialect::DMA::IR::DMADialect
MLIRAnalysis
MLIRIR
MLIRLLVMCommonConversion
MLIRLLVMDialect
MLIRSCFDialect
MLIRTransforms
)
484 changes: 484 additions & 0 deletions codegen/compiler/src/Quidditch/Conversion/ConvertDMAToLLVM.cpp

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions codegen/compiler/src/Quidditch/Conversion/ConvertDMAToLLVM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#pragma once

#include "mlir/Conversion/LLVMCommon/TypeConverter.h"

namespace quidditch {
void populateDMAToLLVMConversionPatterns(mlir::ModuleOp moduleOp,
mlir::LLVMTypeConverter &converter,
mlir::RewritePatternSet &patterns);
}
470 changes: 2 additions & 468 deletions codegen/compiler/src/Quidditch/Conversion/ConvertSnitchToLLVM.cpp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions codegen/compiler/src/Quidditch/Dialect/DMA/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
iree_add_all_subdirs()
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

iree_cc_library(
NAME
DMACoreSpecializationOpInterfaceImpl
HDRS
"DMACoreSpecializationOpInterfaceImpl.h"
SRCS
"DMACoreSpecializationOpInterfaceImpl.cpp"
DEPS
Quidditch::Dialect::Snitch::IR::QuidditchSnitchDialect
Quidditch::Dialect::DMA::IR::DMADialect
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "DMACoreSpecializationOpInterfaceImpl.h"

#include "Quidditch/Dialect/DMA/IR/DMADialect.h"
#include "Quidditch/Dialect/DMA/IR/DMAOps.h"
#include "Quidditch/Dialect/Snitch/IR/QuidditchSnitchInterfaces.h"
#include "mlir/IR/DialectRegistry.h"

using namespace mlir;
using namespace quidditch::dma;
using namespace quidditch::Snitch;

namespace {

//===----------------------------------------------------------------------===//
// StartTransferOp::DMACoreSpecializationOpInterface
//===----------------------------------------------------------------------===//

struct StartTransferOpImpl
: CoreSpecializationOpInterface::ExternalModel<StartTransferOpImpl,
StartTransferOp> {
void replaceWithNoop(Operation *op, RewriterBase &rewriter) const {
rewriter.replaceOpWithNewOp<CompletedTokenOp>(op);
}
};

struct StartTransferOpDMAImpl
: DMACoreSpecializationOpInterface::ExternalModel<StartTransferOpDMAImpl,
StartTransferOp> {};

//===----------------------------------------------------------------------===//
// StartZeroMemTransferOp::DMACoreSpecializationOpInterface
//===----------------------------------------------------------------------===//

struct StartZeroMemTransferOpImpl
: CoreSpecializationOpInterface::ExternalModel<StartZeroMemTransferOpImpl,
StartZeroMemTransferOp> {
void replaceWithNoop(Operation *op, RewriterBase &rewriter) const {
rewriter.replaceOpWithNewOp<CompletedTokenOp>(op);
}

// bool needsSynchronization(Operation *op) const { return true; }
};

struct StartZeroMemTransferOpDMAImpl
: DMACoreSpecializationOpInterface::ExternalModel<
StartZeroMemTransferOpDMAImpl, StartZeroMemTransferOp> {};

//===----------------------------------------------------------------------===//
// WaitForTransfersOpImpl::DMACoreSpecializationOpInterface
//===----------------------------------------------------------------------===//

struct WaitForTransfersOpImpl
: CoreSpecializationOpInterface::ExternalModel<WaitForTransfersOpImpl,
WaitForTransfersOp> {
void replaceWithNoop(Operation *op, RewriterBase &rewriter) const {
rewriter.eraseOp(op);
}

bool needsSynchronization(Operation *op) const { return true; }
};

struct WaitForTransfersOpDMAImpl
: DMACoreSpecializationOpInterface::ExternalModel<WaitForTransfersOpDMAImpl,
WaitForTransfersOp> {};

} // namespace

void quidditch::dma::registerDMACoreSpecializationOpInterface(
mlir::DialectRegistry &registry) {
registry.addExtension(+[](MLIRContext *context, DMADialect *dialect) {
#define REGISTER_IMPLS(Op) Op::attachInterface<Op##Impl, Op##DMAImpl>(*context)
REGISTER_IMPLS(StartTransferOp);
REGISTER_IMPLS(StartZeroMemTransferOp);
REGISTER_IMPLS(WaitForTransfersOp);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#pragma once

namespace mlir {
class DialectRegistry;
}

namespace quidditch::dma {
void registerDMACoreSpecializationOpInterface(mlir::DialectRegistry &registry);
}
73 changes: 73 additions & 0 deletions codegen/compiler/src/Quidditch/Dialect/DMA/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
iree_add_all_subdirs()

iree_cc_library(
NAME
DMADialect
HDRS
"DMADialect.h"
"DMAOps.h"
TEXTUAL_HDRS
"DMAAttrs.cpp.inc"
"DMAAttrs.h.inc"
"DMADialect.cpp.inc"
"DMADialect.h.inc"
"DMAOps.cpp.inc"
"DMAOps.h.inc"
"DMATypes.cpp.inc"
"DMATypes.h.inc"
SRCS
"DMAAttrs.cpp"
"DMADialect.cpp"
"DMAOps.cpp"
"DMATypes.cpp"
DEPS
::DMAAttrsGen
::DMADialectGen
::DMAOpsGen
::DMATypesGen
LLVMSupport
MLIRIR
MLIRInferTypeOpInterface
MLIRSupport
PUBLIC
)

iree_tablegen_library(
NAME
DMAOpsGen
TD_FILE
"DMAOps.td"
OUTS
--gen-op-decls DMAOps.h.inc
--gen-op-defs DMAOps.cpp.inc
)

iree_tablegen_library(
NAME
DMADialectGen
TD_FILE
"DMADialect.td"
OUTS
--gen-dialect-decls DMADialect.h.inc
--gen-dialect-defs DMADialect.cpp.inc
)

iree_tablegen_library(
NAME
DMAAttrsGen
TD_FILE
"DMAAttrs.td"
OUTS
--gen-attrdef-decls DMAAttrs.h.inc
--gen-attrdef-defs DMAAttrs.cpp.inc
)

iree_tablegen_library(
NAME
DMATypesGen
TD_FILE
"DMATypes.td"
OUTS
--gen-typedef-decls DMATypes.h.inc
--gen-typedef-defs DMATypes.cpp.inc
)
1 change: 1 addition & 0 deletions codegen/compiler/src/Quidditch/Dialect/DMA/IR/DMAAttrs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "DMAAttrs.h"
7 changes: 7 additions & 0 deletions codegen/compiler/src/Quidditch/Dialect/DMA/IR/DMAAttrs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#pragma once

#include "mlir/IR/Attributes.h"

#define GET_ATTRDEF_CLASSES
#include "Quidditch/Dialect/DMA/IR/DMAAttrs.h.inc"
20 changes: 20 additions & 0 deletions codegen/compiler/src/Quidditch/Dialect/DMA/IR/DMAAttrs.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef QUIDDITCH_DIALECT_DMA_DMAATTRS
#define QUIDDITCH_DIALECT_DMA_DMAATTRS

include "Quidditch/Dialect/DMA/IR/DMADialect.td"
include "mlir/IR/AttrTypeBase.td"

class DMA_Attr<string name, list<Trait> traits = []> :
AttrDef<DMA_Dialect, name, traits>;

def DMA_CompletedTokenAttr : DMA_Attr<"CompletedToken"> {

let mnemonic = "completed_token";

let description = [{
Attribute representing an instance of a `!dma.token`
signaling a complete transfer.
}];
}

#endif
44 changes: 44 additions & 0 deletions codegen/compiler/src/Quidditch/Dialect/DMA/IR/DMADialect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "DMADialect.h"

#include "DMAAttrs.h"
#include "DMAOps.h"
#include "DMATypes.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/DMA/IR/DMAAttrs.cpp.inc"

#include "Quidditch/Dialect/DMA/IR/DMADialect.cpp.inc"

using namespace mlir;
using namespace quidditch::dma;

//===----------------------------------------------------------------------===//
// DMADialect
//===----------------------------------------------------------------------===//

void DMADialect::initialize() {
addOperations<
#define GET_OP_LIST
#include "Quidditch/Dialect/DMA/IR/DMAOps.cpp.inc"
>();
addAttributes<
#define GET_ATTRDEF_LIST
#include "Quidditch/Dialect/DMA/IR/DMAAttrs.cpp.inc"
>();
addTypes<
#define GET_TYPEDEF_LIST
#include "Quidditch/Dialect/DMA/IR/DMATypes.cpp.inc"
>();
}

Operation *DMADialect::materializeConstant(OpBuilder &builder, Attribute value,
Type type, Location loc) {
if (isa<CompletedTokenAttr>(value))
return builder.create<CompletedTokenOp>(loc);

return nullptr;
}
7 changes: 7 additions & 0 deletions codegen/compiler/src/Quidditch/Dialect/DMA/IR/DMADialect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#pragma once

#include "mlir/IR/Dialect.h"
#include "mlir/IR/Operation.h"

#include "Quidditch/Dialect/DMA/IR/DMADialect.h.inc"
15 changes: 15 additions & 0 deletions codegen/compiler/src/Quidditch/Dialect/DMA/IR/DMADialect.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef QUIDDITCH_DIALECT_SNITCH_DMADIALECT
#define QUIDDITCH_DIALECT_SNITCH_DMADIALECT

include "mlir/IR/DialectBase.td"

def DMA_Dialect : Dialect {
let name = "dma";
let cppNamespace = "::quidditch::dma";

let useDefaultAttributePrinterParser = 1;
let useDefaultTypePrinterParser = 1;
let hasConstantMaterializer = 1;
}

#endif
Loading

0 comments on commit 36ae19f

Please sign in to comment.