From 265c0f720b5903cee8adaa60e8802173b81aaede Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Thu, 2 Nov 2023 00:21:25 -0700 Subject: [PATCH] fixme: initial noisy dialect definition --- lib/Dialect/Noisy/BUILD | 103 +++++++++++++++++++++++++++++ lib/Dialect/Noisy/CMakeLists.txt | 27 ++++++++ lib/Dialect/Noisy/NoisyDialect.cpp | 31 +++++++++ lib/Dialect/Noisy/NoisyDialect.h | 10 +++ lib/Dialect/Noisy/NoisyDialect.td | 15 +++++ lib/Dialect/Noisy/NoisyOps.cpp | 11 +++ lib/Dialect/Noisy/NoisyOps.h | 14 ++++ lib/Dialect/Noisy/NoisyOps.td | 52 +++++++++++++++ lib/Dialect/Noisy/NoisyTypes.h | 9 +++ lib/Dialect/Noisy/NoisyTypes.td | 17 +++++ 10 files changed, 289 insertions(+) create mode 100644 lib/Dialect/Noisy/BUILD create mode 100644 lib/Dialect/Noisy/CMakeLists.txt create mode 100644 lib/Dialect/Noisy/NoisyDialect.cpp create mode 100644 lib/Dialect/Noisy/NoisyDialect.h create mode 100644 lib/Dialect/Noisy/NoisyDialect.td create mode 100644 lib/Dialect/Noisy/NoisyOps.cpp create mode 100644 lib/Dialect/Noisy/NoisyOps.h create mode 100644 lib/Dialect/Noisy/NoisyOps.td create mode 100644 lib/Dialect/Noisy/NoisyTypes.h create mode 100644 lib/Dialect/Noisy/NoisyTypes.td diff --git a/lib/Dialect/Noisy/BUILD b/lib/Dialect/Noisy/BUILD new file mode 100644 index 0000000..bc9947c --- /dev/null +++ b/lib/Dialect/Noisy/BUILD @@ -0,0 +1,103 @@ +load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "td_library") + +package( + default_visibility = ["//visibility:public"], +) + +td_library( + name = "td_files", + srcs = [ + "NoisyDialect.td", + "NoisyOps.td", + "NoisyTypes.td", + ], + deps = [ + "@llvm-project//mlir:BuiltinDialectTdFiles", + "@llvm-project//mlir:InferTypeOpInterfaceTdFiles", + "@llvm-project//mlir:OpBaseTdFiles", + "@llvm-project//mlir:SideEffectInterfacesTdFiles", + ], +) + +gentbl_cc_library( + name = "dialect_inc_gen", + tbl_outs = [ + ( + ["-gen-dialect-decls"], + "NoisyDialect.h.inc", + ), + ( + ["-gen-dialect-defs"], + "NoisyDialect.cpp.inc", + ), + ], + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "NoisyDialect.td", + deps = [ + ":td_files", + ], +) + +gentbl_cc_library( + name = "types_inc_gen", + tbl_outs = [ + ( + ["-gen-typedef-decls"], + "NoisyTypes.h.inc", + ), + ( + ["-gen-typedef-defs"], + "NoisyTypes.cpp.inc", + ), + ], + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "NoisyTypes.td", + deps = [ + ":dialect_inc_gen", + ":td_files", + ], +) + +gentbl_cc_library( + name = "ops_inc_gen", + tbl_outs = [ + ( + ["-gen-op-decls"], + "NoisyOps.h.inc", + ), + ( + ["-gen-op-defs"], + "NoisyOps.cpp.inc", + ), + ], + tblgen = "@llvm-project//mlir:mlir-tblgen", + td_file = "NoisyOps.td", + deps = [ + ":dialect_inc_gen", + ":td_files", + ":types_inc_gen", + ], +) + +cc_library( + name = "Noisy", + srcs = [ + "NoisyDialect.cpp", + "NoisyOps.cpp", + ], + hdrs = [ + "NoisyDialect.h", + "NoisyOps.h", + "NoisyTypes.h", + ], + deps = [ + ":dialect_inc_gen", + ":ops_inc_gen", + ":types_inc_gen", + "@llvm-project//mlir:ComplexDialect", + "@llvm-project//mlir:Dialect", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:InferTypeOpInterface", + "@llvm-project//mlir:Support", + ], +) diff --git a/lib/Dialect/Noisy/CMakeLists.txt b/lib/Dialect/Noisy/CMakeLists.txt new file mode 100644 index 0000000..5e38757 --- /dev/null +++ b/lib/Dialect/Noisy/CMakeLists.txt @@ -0,0 +1,27 @@ +# Inlining `add_mlir_dialect(Noisy noisy)` commands so that +# we can custom name `*.inc` generated files. +set(LLVM_TARGET_DEFINITIONS NoisyOps.td) +mlir_tablegen(NoisyOps.h.inc -gen-op-decls) +mlir_tablegen(NoisyOps.cpp.inc -gen-op-defs) +mlir_tablegen(NoisyTypes.h.inc -gen-typedef-decls -typedefs-dialect=noisy) +mlir_tablegen(NoisyTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=noisy) +mlir_tablegen(NoisyDialect.h.inc -gen-dialect-decls -dialect=noisy) +mlir_tablegen(NoisyDialect.cpp.inc -gen-dialect-defs -dialect=noisy) +add_public_tablegen_target(MLIRNoisyOpsIncGen) +add_dependencies(mlir-headers MLIRNoisyOpsIncGen) + +add_mlir_doc(NoisyDialect NoisyDialect Noisy/ -gen-dialect-doc) + +set(LLVM_TARGET_DEFINITIONS NoisyPatterns.td) +mlir_tablegen(NoisyCanonicalize.cpp.inc -gen-rewriters) +add_public_tablegen_target(MLIRNoisyCanonicalizationIncGen) + +add_mlir_dialect_library(MLIRNoisy + NoisyDialect.cpp + NoisyOps.cpp + + ADDITIONAL_HEADER_DIRS + ${PROJECT_SOURCE_DIR}/lib/Dialect/Noisy + + LINK_LIBS PUBLIC + ) diff --git a/lib/Dialect/Noisy/NoisyDialect.cpp b/lib/Dialect/Noisy/NoisyDialect.cpp new file mode 100644 index 0000000..0a4f5a5 --- /dev/null +++ b/lib/Dialect/Noisy/NoisyDialect.cpp @@ -0,0 +1,31 @@ +#include "lib/Dialect/Noisy/NoisyDialect.h" + +#include "lib/Dialect/Noisy/NoisyOps.h" +#include "lib/Dialect/Noisy/NoisyTypes.h" +#include "mlir/include/mlir/IR/Builders.h" +#include "llvm/include/llvm/ADT/TypeSwitch.h" + +#include "lib/Dialect/Noisy/NoisyDialect.cpp.inc" +#define GET_TYPEDEF_CLASSES +#include "lib/Dialect/Noisy/NoisyTypes.cpp.inc" +#define GET_OP_CLASSES +#include "lib/Dialect/Noisy/NoisyOps.cpp.inc" + +namespace mlir { +namespace tutorial { +namespace noisy { + +void NoisyDialect::initialize() { + addTypes< +#define GET_TYPEDEF_LIST +#include "lib/Dialect/Noisy/NoisyTypes.cpp.inc" + >(); + addOperations< +#define GET_OP_LIST +#include "lib/Dialect/Noisy/NoisyOps.cpp.inc" + >(); +} + +} // namespace noisy +} // namespace tutorial +} // namespace mlir diff --git a/lib/Dialect/Noisy/NoisyDialect.h b/lib/Dialect/Noisy/NoisyDialect.h new file mode 100644 index 0000000..8341085 --- /dev/null +++ b/lib/Dialect/Noisy/NoisyDialect.h @@ -0,0 +1,10 @@ +#ifndef LIB_DIALECT_NOISY_NOISYDIALECT_H_ +#define LIB_DIALECT_NOISY_NOISYDIALECT_H_ + +// Required because the .h.inc file refers to MLIR classes and does not itself +// have any includes. +#include "mlir/include/mlir/IR/DialectImplementation.h" + +#include "lib/Dialect/Noisy/NoisyDialect.h.inc" + +#endif // LIB_DIALECT_NOISY_NOISYDIALECT_H_ diff --git a/lib/Dialect/Noisy/NoisyDialect.td b/lib/Dialect/Noisy/NoisyDialect.td new file mode 100644 index 0000000..954e703 --- /dev/null +++ b/lib/Dialect/Noisy/NoisyDialect.td @@ -0,0 +1,15 @@ +#ifndef LIB_DIALECT_NOISY_NOISYDIALECT_TD_ +#define LIB_DIALECT_NOISY_NOISYDIALECT_TD_ + +include "mlir/IR/OpBase.td" + +def Noisy_Dialect : Dialect { + let name = "noisy"; + let summary = "A dialect for arithmetic on noisy i32s"; + + let cppNamespace = "::mlir::tutorial::noisy"; + + let useDefaultTypePrinterParser = 1; +} + +#endif // LIB_DIALECT_NOISY_NOISYDIALECT_TD_ diff --git a/lib/Dialect/Noisy/NoisyOps.cpp b/lib/Dialect/Noisy/NoisyOps.cpp new file mode 100644 index 0000000..d26c760 --- /dev/null +++ b/lib/Dialect/Noisy/NoisyOps.cpp @@ -0,0 +1,11 @@ +#include "lib/Dialect/Noisy/NoisyOps.h" + +namespace mlir { +namespace tutorial { +namespace noisy { + +// Nothing to do yet + +} // namespace noisy +} // namespace tutorial +} // namespace mlir diff --git a/lib/Dialect/Noisy/NoisyOps.h b/lib/Dialect/Noisy/NoisyOps.h new file mode 100644 index 0000000..0948e91 --- /dev/null +++ b/lib/Dialect/Noisy/NoisyOps.h @@ -0,0 +1,14 @@ +#ifndef LIB_DIALECT_NOISY_NOISYOPS_H_ +#define LIB_DIALECT_NOISY_NOISYOPS_H_ + +#include "lib/Dialect/Noisy/NoisyDialect.h" +#include "lib/Dialect/Noisy/NoisyTypes.h" +#include "mlir/Interfaces/InferTypeOpInterface.h" // from @llvm-project +#include "mlir/include/mlir/IR/BuiltinOps.h" // from @llvm-project +#include "mlir/include/mlir/IR/BuiltinTypes.h" // from @llvm-project +#include "mlir/include/mlir/IR/Dialect.h" // from @llvm-project + +#define GET_OP_CLASSES +#include "lib/Dialect/Noisy/NoisyOps.h.inc" + +#endif // LIB_DIALECT_NOISY_NOISYOPS_H_ diff --git a/lib/Dialect/Noisy/NoisyOps.td b/lib/Dialect/Noisy/NoisyOps.td new file mode 100644 index 0000000..2fcfe5c --- /dev/null +++ b/lib/Dialect/Noisy/NoisyOps.td @@ -0,0 +1,52 @@ +#ifndef LIB_DIALECT_NOISY_NOISYOPS_TD_ +#define LIB_DIALECT_NOISY_NOISYOPS_TD_ + +include "NoisyDialect.td" +include "NoisyTypes.td" +include "mlir/IR/BuiltinAttributes.td" +include "mlir/IR/OpBase.td" +include "mlir/Interfaces/InferTypeOpInterface.td" +include "mlir/Interfaces/SideEffectInterfaces.td" + +// FIXME: can remove all type annotations? +class Noisy_BinOp : Op { + let arguments = (ins Noisy_I32:$lhs, Noisy_I32:$rhs); + let results = (outs Noisy_I32:$output); + let assemblyFormat = "$lhs `,` $rhs attr-dict `:` qualified(type($output))"; +} + +def Noisy_AddOp : Noisy_BinOp<"add"> { + let summary = "Addition operation between noisy ints."; +} + +def Noisy_SubOp : Noisy_BinOp<"sub"> { + let summary = "Subtraction operation between noisy ints."; +} + +def Noisy_MulOp : Noisy_BinOp<"mul"> { + let summary = "Multiplication operation between noisy ints."; +} + +def Noisy_FromI32Op : Op { + let summary = "Creates a noisy i32 from a regular i32, with a given input noise."; + let arguments = (ins I32:$input); + let results = (outs Noisy_I32:$output); + let assemblyFormat = "$input attr-dict `:` type($input) `->` qualified(type($output))"; + let hasFolder = 1; +} + +def Noisy_ToI32Op : Op { + let summary = "Converts a noisy integer to a regular integer, failing if the noise is too high."; + let arguments = (ins Noisy_I32:$input); + let results = (outs I32:$output); + let assemblyFormat = "$input attr-dict `:` qualified(type($input)) `->` type($output)"; +} + +def Noisy_ReduceNoise : Op { + let summary = "Reduces the noise in a noisy integer to a fixed noise level. Expensive!"; + let arguments = (ins Noisy_I32:$input); + let results = (outs Noisy_I32:$output); + let assemblyFormat = "$input attr-dict `:` qualified(type($input)) `->` type($output)"; +} + +#endif // LIB_DIALECT_NOISY_NOISYOPS_TD_ diff --git a/lib/Dialect/Noisy/NoisyTypes.h b/lib/Dialect/Noisy/NoisyTypes.h new file mode 100644 index 0000000..7b432c9 --- /dev/null +++ b/lib/Dialect/Noisy/NoisyTypes.h @@ -0,0 +1,9 @@ +#ifndef LIB_TYPES_NOISY_NOISYTYPES_H_ +#define LIB_TYPES_NOISY_NOISYTYPES_H_ + +#include "mlir/include/mlir/IR/DialectImplementation.h" + +#define GET_TYPEDEF_CLASSES +#include "lib/Dialect/Noisy/NoisyTypes.h.inc" + +#endif // LIB_TYPES_NOISY_NOISYTYPES_H_ diff --git a/lib/Dialect/Noisy/NoisyTypes.td b/lib/Dialect/Noisy/NoisyTypes.td new file mode 100644 index 0000000..e6580a9 --- /dev/null +++ b/lib/Dialect/Noisy/NoisyTypes.td @@ -0,0 +1,17 @@ +#ifndef LIB_DIALECT_NOISY_NOISYTYPES_TD_ +#define LIB_DIALECT_NOISY_NOISYTYPES_TD_ + +include "NoisyDialect.td" +include "mlir/IR/AttrTypeBase.td" + +class Noisy_Type : TypeDef { + let mnemonic = typeMnemonic; +} + +def Noisy_I32 : Noisy_Type<"NoisyI32", "i32"> { + let summary = "A type for approximate 32-bit integers."; + let parameters = (ins "int":$noiseBound); + let assemblyFormat = "`<` $noiseBound `>`"; +} + +#endif // LIB_DIALECT_NOISY_NOISYTYPES_TD_