Skip to content

Commit

Permalink
fixme: initial noisy dialect definition
Browse files Browse the repository at this point in the history
  • Loading branch information
j2kun committed Nov 2, 2023
1 parent 1c276e9 commit 265c0f7
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 0 deletions.
103 changes: 103 additions & 0 deletions lib/Dialect/Noisy/BUILD
Original file line number Diff line number Diff line change
@@ -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",
],
)
27 changes: 27 additions & 0 deletions lib/Dialect/Noisy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
31 changes: 31 additions & 0 deletions lib/Dialect/Noisy/NoisyDialect.cpp
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions lib/Dialect/Noisy/NoisyDialect.h
Original file line number Diff line number Diff line change
@@ -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_
15 changes: 15 additions & 0 deletions lib/Dialect/Noisy/NoisyDialect.td
Original file line number Diff line number Diff line change
@@ -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_
11 changes: 11 additions & 0 deletions lib/Dialect/Noisy/NoisyOps.cpp
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions lib/Dialect/Noisy/NoisyOps.h
Original file line number Diff line number Diff line change
@@ -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_
52 changes: 52 additions & 0 deletions lib/Dialect/Noisy/NoisyOps.td
Original file line number Diff line number Diff line change
@@ -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<string mnemonic> : Op<Noisy_Dialect, mnemonic, [Pure, SameOperandsAndResultType]> {
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<Noisy_Dialect, "from_i32", [Pure]> {
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<Noisy_Dialect, "to_i32", [Pure]> {
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<Noisy_Dialect, "reduce_noise", [Pure]> {
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_
9 changes: 9 additions & 0 deletions lib/Dialect/Noisy/NoisyTypes.h
Original file line number Diff line number Diff line change
@@ -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_
17 changes: 17 additions & 0 deletions lib/Dialect/Noisy/NoisyTypes.td
Original file line number Diff line number Diff line change
@@ -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<string name, string typeMnemonic> : TypeDef<Noisy_Dialect, name> {
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_

0 comments on commit 265c0f7

Please sign in to comment.