Skip to content

Commit

Permalink
[gccjit] add MaterializeRValue pass definition
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Oct 23, 2024
1 parent cc4d23b commit 3b66317
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/mlir-gccjit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
add_subdirectory(IR)

set(LLVM_TARGET_DEFINITIONS Passes.td)
mlir_tablegen(Passes.h.inc -gen-pass-decls -name GCCJIT)
mlir_tablegen(Passes.capi.h.inc -gen-pass-capi-header --prefix GCCJIT)
mlir_tablegen(Passes.capi.cpp.inc -gen-pass-capi-impl --prefix GCCJIT)
add_public_tablegen_target(MLIRGCCJITPassIncGen)

add_mlir_doc(Passes GCCJITPasses ./ -gen-op-doc)
34 changes: 34 additions & 0 deletions include/mlir-gccjit/Passes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Schrodinger ZHU Yifan <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MLIR_GCCJIT_PASSES_H
#define MLIR_GCCJIT_PASSES_H

#include "mlir-gccjit/IR/GCCJITDialect.h"
#include "mlir-gccjit/IR/GCCJITOps.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Pass/Pass.h"

namespace mlir::gccjit {

std::unique_ptr<Pass> createGCCJITMaterializeRValuePass();

#define GEN_PASS_CLASSES
#define GEN_PASS_REGISTRATION
#define GEN_PASS_DECL
#include "mlir-gccjit/Passes.h.inc"

} // namespace mlir::gccjit

#endif // MLIR_GCCJIT_PASSES_H
55 changes: 55 additions & 0 deletions include/mlir-gccjit/Passes.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 Schrodinger ZHU Yifan <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MLIR_GCCJIT_PASSES
#define MLIR_GCCJIT_PASSES

include "mlir/Pass/PassBase.td"

def GCCJITMaterializeRValue : Pass<"gccjit-materialize-rvalue", "::mlir::gccjit::FuncOp"> {
let summary = "Automatically materialize rvalue expressions in GCCJIT dialect";
let description = [{
RValue expressions are lazy in GCCJIT. If not associated with a local variable,
they will never be emitted inplace. Using a nonmaterialized rvalue expression twice will
result in two separate computations. Using a stateful rvalue across an effectful operation
may result in different results. GCCJIT dialect mimic the low-level behavior of GCCJIT on default,
translating rvalue/lvalue expression plainly. However, if one wants to use this dialect similar to
the traditional SSA semantics, this pass will insert nessary materialization for rvalue expressions.

Definition (Constant RValue): An RValue expression is constant if and only if it is created by
a constant operation.

The materialization criteria are as follows:
- If the RValue expression is a mere constant, then it will not be materialized.
- Otherwise:
- if the RValue has a user that is not local to the block, then the expression is materialized.
- Otherwise, inside a block:
- If the RValue has multiple uses, it shall be materialized.
- If the RValue expression is immediately consumed by terminators, then there is no need to materialize.
- Otherwise:
- If the RValue expression is a call to non-pure/const annotated function, then the expression is immediately
materialized.
- If the RValue expression is a load/dereferece from a volatile lvalue or a dereference of a volatile pointer, then the expression
is immediately materialized.
- If the RValue expression's def-use chain overlap with any other effectful operation such as function call, asm block, or assignment
to variable that is not created by the materialization process, then the expression is materialized.

By materialization, we mean that a local variable will be created and singly assigned by the RValue expression. All
future uses of the RValue expression will be replaced by the local variable.
}];
let constructor = "::mlir::gccjit::createGCCJITMaterializeRValuePass()";
let dependentDialects = ["::mlir::gccjit::GCCJITDialect"];
}

#endif // MLIR_GCCJIT_PASSES
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ add_mlir_dialect_library(MLIRGCCJIT
)

add_subdirectory(Translation)
add_subdirectory(Passes)
10 changes: 10 additions & 0 deletions src/Passes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_mlir_dialect_library(MLIRGCCJITPasses
MaterializeRValue.cpp
DEPENDS
MLIRGCCJIT
MLIRGCCJITPassIncGen
LINK_COMPONENTS
Core
LINK_LIBS PUBLIC
MLIRGCCJIT
)
31 changes: 31 additions & 0 deletions src/Passes/MaterializeRValue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 Schrodinger ZHU Yifan <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "mlir-gccjit/Passes.h"
#

using namespace mlir;
using namespace mlir::gccjit;

namespace {
struct GCCJITMaterializeRValue
: public GCCJITMaterializeRValueBase<GCCJITMaterializeRValue> {
using GCCJITMaterializeRValueBase::GCCJITMaterializeRValueBase;
void runOnOperation() override final {}
};
} // namespace

std::unique_ptr<Pass> mlir::gccjit::createGCCJITMaterializeRValuePass() {
return std::make_unique<GCCJITMaterializeRValue>();
}

0 comments on commit 3b66317

Please sign in to comment.