Skip to content

Commit

Permalink
started integrating the tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Gysi committed Dec 15, 2019
1 parent 6a7fc22 commit b4eeef0
Show file tree
Hide file tree
Showing 29 changed files with 1,225 additions and 279 deletions.
26 changes: 8 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,15 @@ function(mlir_tablegen ofn)
endfunction()

function(whole_archive_link target)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(link_flags "-L${CMAKE_BINARY_DIR}/lib ")
FOREACH(LIB ${ARGN})
string(CONCAT link_flags ${link_flags} "-Wl,-force_load ${CMAKE_BINARY_DIR}/lib/lib${LIB}.a ")
ENDFOREACH(LIB)
elseif(MSVC)
FOREACH(LIB ${ARGN})
string(CONCAT link_flags ${link_flags} "/WHOLEARCHIVE:${LIB} ")
ENDFOREACH(LIB)
else()
set(link_flags "-L${LLVM_LIBRARY_DIR} -Wl,--whole-archive,")
FOREACH(LIB ${ARGN})
string(CONCAT link_flags ${link_flags} "-l${LIB},")
ENDFOREACH(LIB)
string(CONCAT link_flags ${link_flags} "--no-whole-archive")
endif()
set(link_flags "-L${LLVM_LIBRARY_DIR} -Wl,--whole-archive,")
FOREACH(LIB ${ARGN})
string(CONCAT link_flags ${link_flags} "-l${LIB},")
ENDFOREACH(LIB)
string(CONCAT link_flags ${link_flags} "--no-whole-archive")
set_target_properties(${target} PROPERTIES LINK_FLAGS ${link_flags})
endfunction(whole_archive_link)

add_subdirectory(include/sten)
add_subdirectory(lib)
# TODO build all libraries in ./lib and add ./lib to the linker path in the while_archive_linknin
add_subdirectory(include/Dialect/Stencil)
add_subdirectory(lib/Dialect/Stencil)
add_subdirectory(tools/oec-opt)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# stencil-dialect

Development repository for the open earth compiler. Buld llvm and mlir in a separate folder and generate a build folder using the following commands:
Development repository for the open earth compiler. The repository depends on a build of llvm and mlir. Before building mlir register your custom dialects in include/mlir/IR/DialectSymbolRegistry.def. Once the llvm and mlir are built setup configure the project using the following commands.

```
mkdir build && cd build
Expand Down
8 changes: 8 additions & 0 deletions include/Dialect/Stencil/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(LLVM_TARGET_DEFINITIONS StencilOps.td)

# Generate the class interfaces
mlir_tablegen(StencilOps.h.inc -gen-op-decls)
# Generate the actual implementation
mlir_tablegen(StencilOps.cpp.inc -gen-op-defs)

add_public_tablegen_target(MLIRStencilOpsIncGen)
12 changes: 12 additions & 0 deletions include/Dialect/Stencil/Passes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef MLIR_DIALECT_STENCIL_PASSES_H
#define MLIR_DIALECT_STENCIL_PASSES_H

#include "mlir/Pass/Pass.h"

namespace mlir {
namespace stencil {

} // namespace stencil
} // namespace mlir

#endif // MLIR_DIALECT_STENCIL_PASSES_H
54 changes: 54 additions & 0 deletions include/Dialect/Stencil/StencilBase.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef Stencil_BASE
#define Stencil_BASE

#ifndef OP_BASE
include "mlir/IR/OpBase.td"
#endif // OP_BASE

//===----------------------------------------------------------------------===//
// Stencil dialect definition
//===----------------------------------------------------------------------===//

def Stencil_Dialect : Dialect {
let name = "stencil";

let description = [{
A simple stencil dialect in MLIR.
}];

let cppNamespace = "stencil";
}

//===----------------------------------------------------------------------===//
// Stencil type definitions
//===----------------------------------------------------------------------===//

def Stencil_IsViewType : CPred<"$_self.isa<::mlir::stencil::ViewType>()">;
def Stencil_IsFieldType : CPred<"$_self.isa<::mlir::stencil::FieldType>()">;

def Stencil_View : Type<Stencil_IsViewType, "a view on a field">;
def Stencil_Field : Type<Stencil_IsFieldType, "a field">;
def Stencil_ElementType : AnyTypeOf<[I64, F64]>;

//===----------------------------------------------------------------------===//
// Stencil op definition
//===----------------------------------------------------------------------===//

// Base class for all Stencil ops.
class Stencil_Op<string mnemonic, list<OpTrait> traits = []> :
Op<Stencil_Dialect, mnemonic, traits> {

// For each Stencil op, the following static functions need to be defined in
// StencilOps.cpp:
//
// * static ParseResult parse<op-c++-class-name>(OpAsmParser &parser,
// OperationState &state);
// * static void print(OpAsmPrinter &p, <op-c++-class-name> op)
// * static LogicalResult verify(<op-c++-class-name> op)
let parser = [{ return ::parse$cppClass(parser, result); }];
let printer = [{ ::print(*this, p); }];
let verifier = [{ return ::verify(*this); }];
}


#endif // Stencil_BASE
38 changes: 38 additions & 0 deletions include/Dialect/Stencil/StencilDialect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef MLIR_DIALECT_STENCIL_STENCILDIALECT_H
#define MLIR_DIALECT_STENCIL_STENCILDIALECT_H

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


namespace mlir {
namespace stencil {

class StencilDialect : public Dialect {
public:
explicit StencilDialect(MLIRContext *context);

/// Returns the prefix used in the textual IR to refer to Stencil operations
static StringRef getDialectNamespace() { return "stencil"; }

static StringRef getStencilFunctionAttrName() { return "stencil.function"; }
static StringRef getStencilProgramAttrName() { return "stencil.program"; }

static bool isStencilFunction(FuncOp funcOp) {
return !!funcOp.getAttr(getStencilFunctionAttrName());
}
static bool isStencilProgram(FuncOp funcOp) {
return !!funcOp.getAttr(getStencilProgramAttrName());
}

/// Parses a type registered to this dialect
Type parseType(DialectAsmParser &parser) const override;

/// Prints a type registered to this dialect
void printType(Type type, DialectAsmPrinter &os) const override;
};

} // namespace stencil
} // namespace mlir

#endif // MLIR_DIALECT_STENCIL_STENCILDIALECT_H
17 changes: 17 additions & 0 deletions include/Dialect/Stencil/StencilOps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef MLIR_DIALECT_STENCIL_STENCILOPS_H
#define MLIR_DIALECT_STENCIL_STENCILOPS_H

#include "mlir/IR/Function.h"
#include "Dialect/Stencil/StencilTypes.h"

namespace mlir {
namespace stencil {

/// Retrieve the class declarations generated by TableGen
#define GET_OP_CLASSES
#include "Dialect/Stencil/StencilOps.h.inc"

} // namespace stencil
} // namespace mlir

#endif // MLIR_DIALECT_STENCIL_STENCILOPS_H
134 changes: 134 additions & 0 deletions include/Dialect/Stencil/StencilOps.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#ifndef STENCIL_OPS
#define STENCIL_OPS

#ifndef STENCIL_BASE
include "Dialect/Stencil/StencilBase.td"
#endif // STENCIL_BASE

def Stencil_AccessOp : Stencil_Op<"access"> {
let summary = "view access operation";
let description = [{
This operation takes a view as an input as well as an offset
attribute and return the corresponding element from the view's
underlying field. The offset is specified relatively to the
current position.

Example:

%0 = stencil.access %view[-1, 0, 0] : !stencil.view<?x?x?xf64>
}];

let arguments = (ins Stencil_View:$view, I64ArrayAttr:$offset);
let results = (outs Stencil_ElementType:$res);

let builders = [OpBuilder<"Builder *, OperationState &, Value *,"
"ArrayRef<int64_t>">];

let extraClassDeclaration = [{
static StringRef getOffsetAttrName() { return "offset"; }
SmallVector<int64_t, 3> getOffset() {
SmallVector<int64_t, 3> offsetAttr;
for(auto &attr : offset().cast<ArrayAttr>().getValue()) {
offsetAttr.push_back(attr.cast<IntegerAttr>().getValue().getSExtValue());
}
return offsetAttr;
}
}];
}

def Stencil_LoadOp : Stencil_Op<"load"> {
let arguments = (ins Stencil_Field:$field);
let results = (outs Stencil_View:$res);

let builders = [OpBuilder<"Builder *, OperationState &, Value *,"
"ArrayRef<int64_t> = {-1, -1, -1}">];

let extraClassDeclaration = [{
stencil::ViewType getResultViewType() {
return res()->getType().cast<stencil::ViewType>();
}
}];
}

def Stencil_StoreOp : Stencil_Op<"store"> {
let arguments = (ins Stencil_Field:$field, Stencil_View:$view);
let results = (outs);

let extraClassDeclaration = [{
stencil::FieldType getFieldType() {
return field()->getType().cast<stencil::FieldType>();
}
stencil::ViewType getViewType() {
return view()->getType().cast<stencil::ViewType>();
}
}];
}

def Stencil_ApplyOp : Stencil_Op<"apply"> {
let arguments = (ins SymbolRefAttr:$callee, Variadic<AnyType>:$operands);
let results = (outs Stencil_View:$res);

let builders = [
OpBuilder<"Builder *builder, OperationState &result, FuncOp callee,"
"stencil::ViewType viewType, ArrayRef<Value *> operands = {}">];

let extraClassDeclaration = [{
stencil::ViewType getResultViewType() {
return res()->getType().cast<stencil::ViewType>();
}

static StringRef getCalleeAttrName() { return "callee"; }
FunctionType getCalleeType();
FuncOp getCallee() {
return getParentOfType<ModuleOp>().lookupSymbol<FuncOp>(callee().getLeafReference());
}

/// Get the argument operands to the called function.
operand_range getArgOperands() {
return {arg_operand_begin(), arg_operand_end()};
}

operand_iterator arg_operand_begin() { return operand_begin(); }
operand_iterator arg_operand_end() { return operand_end(); }
}];
}

def Stencil_CallOp : Stencil_Op<"call"> {
let arguments = (ins SymbolRefAttr:$callee, I64ArrayAttr:$offset, Variadic<AnyType>:$operands);
let results = (outs Stencil_ElementType:$res);

let builders = [
OpBuilder<"Builder *builder, OperationState &result, FuncOp callee,"
"stencil::ViewType viewType, ArrayRef<int64_t> offset,"
"ArrayRef<Value *> operands = {}">];

let extraClassDeclaration = [{
stencil::ViewType getResultViewType() {
return res()->getType().cast<stencil::ViewType>();
}

static StringRef getCalleeAttrName() { return "callee"; }
FunctionType getCalleeType();
FuncOp getCallee() {
return getParentOfType<ModuleOp>().lookupSymbol<FuncOp>(callee().getLeafReference());
}
static StringRef getOffsetAttrName() { return "offset"; }
SmallVector<int64_t, 3> getOffset() {
SmallVector<int64_t, 3> offsetAttr;
for(auto &attr : offset().cast<ArrayAttr>().getValue()) {
offsetAttr.push_back(attr.cast<IntegerAttr>().getValue().getSExtValue());
}
return offsetAttr;
}

/// Get the argument operands to the called function.
operand_range getArgOperands() {
return {arg_operand_begin(), arg_operand_end()};
}

operand_iterator arg_operand_begin() { return operand_begin(); }
operand_iterator arg_operand_end() { return operand_end(); }
}];
}

#endif // STENCIL_OPS
69 changes: 69 additions & 0 deletions include/Dialect/Stencil/StencilTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef MLIR_DIALECT_STENCIL_STENCILTYPES_H
#define MLIR_DIALECT_STENCIL_STENCILTYPES_H

#include "mlir/IR/TypeSupport.h"
#include "mlir/IR/Types.h"

namespace mlir {
namespace stencil {

enum StencilTypes {
Field = Type::FIRST_STENCIL_TYPE,
View,
LAST_USED_STENCIL_TYPE = View
};

struct FieldTypeStorage;
class FieldType : public Type::TypeBase<FieldType, Type, FieldTypeStorage> {
public:
// Used for generic hooks in TypeBase.
using Base::Base;

/// Construction hook.
///
/// Create a field type of given `shape` containing elements of type
/// `elementType`.
///
/// Note: `shape` must contain 3 elements, -1 being used to specify an unknown
/// size.
static FieldType get(MLIRContext *context, Type elementType,
ArrayRef<int64_t> shape);

/// Used to implement LLVM-style casts.
static bool kindof(unsigned kind) { return kind == StencilTypes::Field; }

/// Return the type of the field elements.
Type getElementType();
/// Return the shape of the field.
ArrayRef<int64_t> getShape();
};

struct ViewTypeStorage;
class ViewType : public Type::TypeBase<ViewType, Type, ViewTypeStorage> {
public:
// Used for generic hooks in TypeBase.
using Base::Base;

/// Construction hook.
///
/// Create a field type of given `shape` containing elements of type
/// `elementType`.
///
/// Note: `shape` must contain 3 elements, -1 being used to specify an unknown
/// size.
static ViewType get(MLIRContext *context, Type elementType,
ArrayRef<int64_t> shape);

/// Used to implement LLVM-style casts.
static bool kindof(unsigned kind) { return kind == StencilTypes::View; }

/// Return the type of the field elements.
Type getElementType();
/// Return the shape of the field.
ArrayRef<int64_t> getShape();
};

} // namespace stencil
} // namespace mlir

#endif // MLIR_DIALECT_STENCIL_STENCILTYPES_H
10 changes: 0 additions & 10 deletions include/sten/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit b4eeef0

Please sign in to comment.