Skip to content

Commit

Permalink
Initial import of iree-dialects.
Browse files Browse the repository at this point in the history
We plan on using these dialects "natively" as part of the npcomp backend
contract, and provide feedback to evolve them in IREE. Roughly speaking,
we can consider these dialects as "what's missing from upstream that we
think belongs in the general abstraction layer that npcomp's backend
contract targets".

We integrate them by just copying the relevant directory from the IREE
source tree (with `build_tools/update_iree_dialects.sh`). This avoids
adding IREE as a submodule, which is way too heavyweight (including
IREE itself, another copy of LLVM, TensorFlow, ...) and would give the
false impression of a source dependency rather than the lightweight (and
eventually versioned/stabilized) IR-level compatibility that we strive
for.
  • Loading branch information
silvasean committed Aug 11, 2021
1 parent 37df45d commit 0b7dbf5
Show file tree
Hide file tree
Showing 33 changed files with 1,365 additions and 0 deletions.
16 changes: 16 additions & 0 deletions build_tools/update_iree_dialects.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
set -euo pipefail

if [ "$#" -ne 1 ]; then
echo "Usage: $0 <iree_src_root>"
echo 'Description:
iree_src_root: root directory of IREE source checkout
'
exit 1
fi

npcomp_src_root="$(realpath $(dirname $0)/..)"
iree_src_root=$1

rm -rf "${npcomp_src_root}/external/iree-dialects"
cp -a "${iree_src_root}/llvm-external-projects/iree-dialects" "${npcomp_src_root}/external"
1 change: 1 addition & 0 deletions external/iree-dialects/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build/
84 changes: 84 additions & 0 deletions external/iree-dialects/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "td_library")

package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)

exports_files(glob(["include/iree-dialects/Dialect/IREE/*.td"]))

filegroup(
name = "TdFilegroup",
srcs = glob(["include/iree-dialects/Dialect/IREE/*.td"]),
)

td_library(
name = "TdFiles",
srcs = glob(["include/iree-dialects/Dialect/IREE/*.td"]),
includes = ["include"],
deps = [
"@llvm-project//mlir:OpBaseTdFiles",
"@llvm-project//mlir:SideEffectTdFiles",
],
)

gentbl_cc_library(
name = "IREEOpsIncGen",
strip_include_prefix = "include",
tbl_outs = [
(
["-gen-dialect-decls"],
"include/iree-dialects/Dialect/IREE/IREEOpsDialect.h.inc",
),
(
["-gen-dialect-defs"],
"include/iree-dialects/Dialect/IREE/IREEOpsDialect.cpp.inc",
),
(
["-gen-op-decls"],
"include/iree-dialects/Dialect/IREE/IREEOps.h.inc",
),
(
["-gen-op-defs"],
"include/iree-dialects/Dialect/IREE/IREEOps.cpp.inc",
),
(
["-gen-typedef-decls"],
"include/iree-dialects/Dialect/IREE/IREEOpsTypes.h.inc",
),
(
["-gen-typedef-defs"],
"include/iree-dialects/Dialect/IREE/IREEOpsTypes.cpp.inc",
),
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "include/iree-dialects/Dialect/IREE/IREEOps.td",
deps = [":TdFiles"],
)

cc_library(
name = "IREEDialect",
srcs = glob([
"lib/Dialect/IREE/*.cpp",
]),
hdrs = glob(["include/iree-dialects/Dialect/IREE/*.h"]),
includes = ["include"],
deps = [
":IREEOpsIncGen",
"@llvm-project//mlir:IR",
],
)

cc_library(
name = "CAPI",
srcs = [
"lib/CAPI/Dialects.cpp",
],
hdrs = [
"include/iree-dialects-c/Dialects.h",
],
deps = [
":IREEDialect",
"@llvm-project//mlir:CAPIIR",
],
)
70 changes: 70 additions & 0 deletions external/iree-dialects/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
message(FATAL_ERROR
"This project is intended to be built as part of LLVM via "
"-DLLVM_EXTERNAL_PROJECTS=iree-dialects "
"-DLLVM_EXTERNAL_IREE_DIALECTS_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}")
endif()

option(MLIR_ENABLE_BINDINGS_PYTHON "Enables MLIR Python Bindings" OFF)

set(IREE_DIALECTS_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(IREE_DIALECTS_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
message(STATUS "Building iree-dialects project at ${IREE_DIALECTS_SOURCE_DIR} (into ${IREE_DIALECTS_BINARY_DIR})")

# TODO: Fix this upstream so that global include directories are not needed.
set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir)
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include)
set(MLIR_GENERATED_INCLUDE_DIR ${LLVM_BINARY_DIR}/tools/mlir/include)

# TODO: Needed for tablegen. Remove.
include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
include_directories(SYSTEM ${MLIR_GENERATED_INCLUDE_DIR})
include_directories(SYSTEM ${IREE_DIALECTS_SOURCE_DIR}/include)

function(iree_dialects_target_includes target)
set(_dirs
$<BUILD_INTERFACE:${MLIR_INCLUDE_DIR}>
$<BUILD_INTERFACE:${MLIR_GENERATED_INCLUDE_DIR}>
$<BUILD_INTERFACE:${IREE_DIALECTS_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${IREE_DIALECTS_BINARY_DIR}/include>
)
# In LLVM parlance, the actual target may just be an interface and may not
# be responsible for actually compiling anything. The corresponding obj.
# target, when present, is just used for compilation and does not
# contribute to the interface properties.
# TODO: Normalize this upstream.
target_include_directories(${target} PUBLIC ${_dirs})
if(TARGET obj.${target})
target_include_directories(obj.${target} PRIVATE ${_dirs})
endif()
endfunction()

# Configure CMake and tablegen.
list(APPEND CMAKE_MODULE_PATH ${MLIR_MAIN_SRC_DIR}/cmake/modules)
list(APPEND CMAKE_MODULE_PATH ${LLVM_MAIN_SRC_DIR}/cmake)
set(MLIR_TABLEGEN_EXE mlir-tblgen)

include(TableGen)
include(AddLLVM)
include(AddMLIR)

################################################################################
# Setup python.
# TODO: Make one upstream macro to do this.
################################################################################

if(MLIR_ENABLE_BINDINGS_PYTHON)
include(MLIRDetectPythonEnv)
mlir_detect_pybind11_install()
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
COMPONENTS Interpreter Development NumPy REQUIRED)
find_package(pybind11 2.6 CONFIG REQUIRED)
endif()

add_subdirectory(include)
add_subdirectory(lib)
add_subdirectory(test)

if(MLIR_ENABLE_BINDINGS_PYTHON)
add_subdirectory(python)
endif()
11 changes: 11 additions & 0 deletions external/iree-dialects/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# IREE Dialects Project

Sources for IREE's public dialects (containing ops/types/attributes that are
unique to IREE and can appear in compiler inputs).

This project is intended to be used via LLVM's external projects setup:

* `-DLLVM_EXTERNAL_PROJECTS=iree-dialects`
* `-DLLVM_EXTERNAL_IREE_DIALECTS_SOURCE_DIR={this_directory}`

It depends on the `mlir` project.
27 changes: 27 additions & 0 deletions external/iree-dialects/build_tools/build_standalone.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
# Copyright 2021 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Simple script that does a CMake configure of this project as an external
# LLVM project so it can be tested in isolation to larger assemblies.
# This is meant for CI's and project maintainers.

set -eu -o errtrace

project_dir="$(cd $(dirname $0)/.. && pwd)"
repo_root="$(cd "$project_dir"/../.. && pwd)"
llvm_project_dir="$repo_root/third_party/llvm-project"
build_dir="$project_dir/build"

cmake -GNinja -B"$build_dir" "$llvm_project_dir/llvm" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_PROJECTS=mlir \
-DLLVM_EXTERNAL_PROJECTS=iree-dialects \
-DLLVM_EXTERNAL_IREE_DIALECTS_SOURCE_DIR="$project_dir" \
-DMLIR_ENABLE_BINDINGS_PYTHON=ON

cd "$build_dir"
ninja tools/iree-dialects/all
1 change: 1 addition & 0 deletions external/iree-dialects/include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(iree-dialects)
22 changes: 22 additions & 0 deletions external/iree-dialects/include/iree-dialects-c/Dialects.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef IREE_LLVM_EXTERNAL_PROJECTS_IREE_DIALECTS_C_DIALECTS_H
#define IREE_LLVM_EXTERNAL_PROJECTS_IREE_DIALECTS_C_DIALECTS_H

#include "mlir-c/Registration.h"

#ifdef __cplusplus
extern "C" {
#endif

MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(IREE, iree);

#ifdef __cplusplus
}
#endif

#endif // IREE_LLVM_EXTERNAL_PROJECTS_IREE_DIALECTS_C_DIALECTS_H
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(Dialect)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(IREE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_mlir_dialect(IREEOps iree)
add_mlir_doc(IREEDialect IREEDialect IREE/ -gen-dialect-doc)
add_mlir_doc(IREEOps IREEOps IREE/ -gen-op-doc)
115 changes: 115 additions & 0 deletions external/iree-dialects/include/iree-dialects/Dialect/IREE/IREEBase.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2021 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef IREE_LLVM_EXTERNAL_PROJECTS_IREE_DIALECTS_DIALECT_IREE_IREE_BASE_TD
#define IREE_LLVM_EXTERNAL_PROJECTS_IREE_DIALECTS_DIALECT_IREE_IREE_BASE_TD

include "mlir/IR/OpBase.td"
include "mlir/Interfaces/SideEffectInterfaces.td"

def IREE_Dialect : Dialect {
let name = "iree";
let summary = "Public ops/type/attributes legal for input to IREE's compiler";
let description = [{
IREE's compiler allows as input a number of common dialects. This dialect
contains structural and unique ops that do not exist elsewhere or that IREE
has an interest in maintaining as a stable set.

The contents of this dialect often mirror various constructs in IREE's
internal implementation. The focus here is on simplicity and stability
over time. Generally, this dialect does not use "advanced" features and
should be broadly source compatible over a range of LLVM versions. There
are of course, limits, and source-compatibility is not guaranteed, since
LLVM/MLIR's API surface is itself unstable.
}];
let cppNamespace = "::mlir::iree";
}

class IREE_Op<string mnemonic, list<OpTrait> traits = []> :
Op<IREE_Dialect, mnemonic, traits>;
class IREE_PureOp<string mnemonic, list<OpTrait> traits = []> :
Op<IREE_Dialect, mnemonic, !listconcat(traits, [NoSideEffect])>;
class IREE_Type<string name> : TypeDef<IREE_Dialect, name>;

//===----------------------------------------------------------------------===//
// Predicates
//===----------------------------------------------------------------------===//

class IREE_AliasedSymbolRefAttr : Attr<CPred<"$_self.isa<FlatSymbolRefAttr>()">,
"symbol reference attribute"> {
let storageType = [{ FlatSymbolRefAttr }];
let returnType = [{ StringRef }];
let valueType = NoneType;
let constBuilderCall = "$_builder.getSymbolRefAttr($0)";
}

class IREE_AnyPtrOf<list<Type> types> :
Type<And<[
CPred<"$_self.isa<::mlir::iree::PtrType>()">,
Or<!foreach(type, types,
SubstLeaves<
"$_self",
"$_self.cast<::mlir::iree::PtrType>().getTargetType()",
type.predicate>)>,
]>, !interleave(!foreach(type, types, type.summary), " or ")> {
string builderCall = "";
}

def IREE_PrimitiveType : AnyTypeOf<[Index, AnySignlessInteger, AnyFloat]>;
def IREE_Tensor : TypeAlias<AnyRankedTensor>;

def IREE_AnyList : DialectType<
IREE_Dialect,
CPred<"$_self.isa<::mlir::iree::ListType>()">,
"list"> {
let description = [{
A mutable, resizable list of some type.
}];
}

class IREE_ListOf<Type type> :
Type<And<[
CPred<"$_self.isa<::mlir::iree::ListType>()">,
SubstLeaves<"$_self",
"$_self.cast<::mlir::iree::ListType>().getElementType()",
type.predicate>
]>, "list<" # type.summary # ">"> {
// Set the builder call if the base type has a builder call.
string builderCall = !if(!empty(type.builderCall),
"", "::mlir::iree::ListType::get(" # type.builderCall # ")");
}

def IREE_ElementTypeParameter : TypeParameter<
"::mlir::Type", "A type suitable as an element type of a container">;
def IREE_PtrTargetTypeParameter : TypeParameter<
"::mlir::Type", "A type suitable as a target type of a pointer">;

def IREE_Dim : TypeAlias<Index>;
def IREE_Dims : Variadic<IREE_Dim>;
def IREE_Shape : Variadic<IREE_Dim>;
def IREE_ShapeDynamicDims : Variadic<IREE_Dim>;

def IREE_VariableRefAttr : IREE_AliasedSymbolRefAttr;
def IREE_VariablePtr : IREE_AnyPtrOf<[IREE_Tensor, IREE_PrimitiveType]>;

class IREE_IndexAttrBase<string descr> :
TypedAttrBase<
Index, "IntegerAttr",
And<[
CPred<"$_self.isa<IntegerAttr>()">,
CPred<"$_self.cast<IntegerAttr>().getType().isIndex()">,
]>,
descr> {
let returnType = [{ APInt }];
}
def IREE_IndexAttr : IREE_IndexAttrBase<"size_t">;

def IREE_TiedOpStorageAttr :
TypedArrayAttrBase<IREE_IndexAttr, "64-bit integer array attribute"> {
let constBuilderCall = "$_builder.getI64ArrayAttr($0)";
}

#endif // IREE_LLVM_EXTERNAL_PROJECTS_IREE_DIALECTS_DIALECT_IREE_IREE_BASE_TD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef IREE_LLVM_EXTERNAL_PROJECTS_IREE_DIALECTS_DIALECT_IREE_IREE_DIALECT_H
#define IREE_LLVM_EXTERNAL_PROJECTS_IREE_DIALECTS_DIALECT_IREE_IREE_DIALECT_H

#include "mlir/IR/Dialect.h"

// Include generated dialect code (this comment blocks clang-format from
// clobbering order).
#include "iree-dialects/Dialect/IREE/IREEOpsDialect.h.inc"

#define GET_TYPEDEF_CLASSES
#include "iree-dialects/Dialect/IREE/IREEOpsTypes.h.inc"

#endif // IREE_LLVM_EXTERNAL_PROJECTS_IREE_DIALECTS_DIALECT_IREE_IREE_DIALECT_H
Loading

0 comments on commit 0b7dbf5

Please sign in to comment.