//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===// // // Part of the LLVM Project, 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 // //===----------------------------------------------------------------------===// // // This transformation pass performs a sparse conditional constant propagation // in MLIR. It identifies values known to be constant, propagates that // information throughout the IR, and replaces them. This is done with an // optimistic dataflow analysis that assumes that all values are constant until // proven otherwise. // //===----------------------------------------------------------------------===// #include "mlir/Transforms/Passes.h" #include "mlir/Analysis/DataFlow/ConstantPropagationAnalysis.h" #include "mlir/Analysis/DataFlow/DeadCodeAnalysis.h" #include "mlir/IR/Builders.h" #include "mlir/IR/Dialect.h" #include "mlir/Interfaces/SideEffectInterfaces.h" #include "mlir/Pass/Pass.h" #include "mlir/Transforms/FoldUtils.h" namespace mlir { #define GEN_PASS_DEF_SCCP #include "mlir/Transforms/Passes.h.inc" } // namespace mlir usingnamespacemlir; usingnamespacemlir::dataflow; //===----------------------------------------------------------------------===// // SCCP Rewrites //===----------------------------------------------------------------------===// /// Replace the given value with a constant if the corresponding lattice /// represents a constant. Returns success if the value was replaced, failure /// otherwise. static LogicalResult replaceWithConstant(DataFlowSolver &solver, OpBuilder &builder, OperationFolder &folder, Value value) { … } /// Rewrite the given regions using the computing analysis. This replaces the /// uses of all values that have been computed to be constant, and erases as /// many newly dead operations. static void rewrite(DataFlowSolver &solver, MLIRContext *context, MutableArrayRef<Region> initialRegions) { … } //===----------------------------------------------------------------------===// // SCCP Pass //===----------------------------------------------------------------------===// namespace { struct SCCP : public impl::SCCPBase<SCCP> { … }; } // namespace void SCCP::runOnOperation() { … } std::unique_ptr<Pass> mlir::createSCCPPass() { … }