Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement simple loop-invariant-code-motion based on dialect interfaces.
PiperOrigin-RevId: 275004258
- Loading branch information
1 parent
98f64b4
commit b843cc5
Showing
15 changed files
with
1,166 additions
and
512 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//===- LoopLikeInterface.h - Loop-like operations interface ---------------===// | ||
// | ||
// Copyright 2019 The MLIR Authors. | ||
// | ||
// 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. | ||
// ============================================================================= | ||
// | ||
// This file implements the operation interface for loop like operations. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_TRANSFORMS_LOOPLIKEINTERFACE_H_ | ||
#define MLIR_TRANSFORMS_LOOPLIKEINTERFACE_H_ | ||
|
||
#include "mlir/IR/OpDefinition.h" | ||
#include "mlir/Support/LogicalResult.h" | ||
#include "llvm/ADT/ArrayRef.h" | ||
|
||
namespace mlir { | ||
|
||
#include "mlir/Transforms/LoopLikeInterface.h.inc" | ||
|
||
} // namespace mlir | ||
|
||
#endif // MLIR_TRANSFORMS_LOOPLIKEINTERFACE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//===- LoopLikeInterface.td - LoopLike interface -----------*- tablegen -*-===// | ||
// | ||
// Copyright 2019 The MLIR Authors. | ||
// | ||
// 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. | ||
// ============================================================================= | ||
// | ||
// Defines the interface for loop-like operations as used by LICM. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifdef MLIR_LOOPLIKEINTERFACE | ||
#else | ||
#define MLIR_LOOPLIKEINTERFACE | ||
|
||
#ifdef OP_BASE | ||
#else | ||
include "mlir/IR/OpBase.td" | ||
#endif // OP_BASE | ||
|
||
def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> { | ||
let description = [{ | ||
Encodes properties of a loop. Operations that implement this interface will | ||
be considered by loop-invariant code motion. | ||
}]; | ||
|
||
let methods = [ | ||
InterfaceMethod<[{ | ||
Returns true if the given value is defined outside of the loop. | ||
A sensible implementation could be to check whether the value's defining | ||
operation lies outside of the loops body region. If the loop uses | ||
explicit capture of dependencies, an implementation could check whether | ||
the value corresponds to a captured dependency. | ||
}], | ||
"bool", "isDefinedOutsideOfLoop", (ins "Value *":$value) | ||
>, | ||
InterfaceMethod<[{ | ||
Returns the region that makes up the body of the loop and should be | ||
inspected for loop-invariant operations. | ||
}], | ||
"Region &", "getLoopBody" | ||
>, | ||
InterfaceMethod<[{ | ||
Moves the given vector of operations out of the loop. The vector is | ||
sorted topologically. | ||
}], | ||
"LogicalResult", "moveOutOfLoop", (ins "ArrayRef<Operation *>":$ops) | ||
>, | ||
]; | ||
} | ||
|
||
#endif // MLIR_LOOPLIKEINTERFACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//===- SideEffectsInterface.h - dialect interface modeling side effects ---===// | ||
// | ||
// Copyright 2019 The MLIR Authors. | ||
// | ||
// 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. | ||
// ============================================================================= | ||
// | ||
// This file specifies a dialect interface to model side-effects. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_TRANSFORMS_SIDEEFFECTSINTERFACE_H_ | ||
#define MLIR_TRANSFORMS_SIDEEFFECTSINTERFACE_H_ | ||
|
||
#include "mlir/IR/DialectInterface.h" | ||
#include "mlir/IR/Operation.h" | ||
|
||
namespace mlir { | ||
|
||
/// Specifies an interface for basic side-effect modelling that is used by the | ||
/// loop-invariant code motion pass. | ||
/// | ||
/// TODO: This interface should be replaced by a more general solution. | ||
class SideEffectsDialectInterface | ||
: public DialectInterface::Base<SideEffectsDialectInterface> { | ||
public: | ||
SideEffectsDialectInterface(Dialect *dialect) : Base(dialect) {} | ||
|
||
enum SideEffecting { | ||
Never, /* the operation has no side-effects */ | ||
Recursive, /* the operation has side-effects if a contained operation has */ | ||
Always /* the operation has side-effects */ | ||
}; | ||
|
||
/// Checks whether the given operation has side-effects. | ||
virtual SideEffecting isSideEffecting(Operation *op) const { | ||
if (op->hasNoSideEffect()) | ||
return Never; | ||
return Always; | ||
}; | ||
}; | ||
|
||
class SideEffectsInterface | ||
: public DialectInterfaceCollection<SideEffectsDialectInterface> { | ||
public: | ||
using SideEffecting = SideEffectsDialectInterface::SideEffecting; | ||
explicit SideEffectsInterface(MLIRContext *ctx) | ||
: DialectInterfaceCollection<SideEffectsDialectInterface>(ctx) {} | ||
|
||
SideEffecting isSideEffecting(Operation *op) const { | ||
// First check generic trait. | ||
if (op->hasNoSideEffect()) | ||
return SideEffecting::Never; | ||
if (auto handler = getInterfaceFor(op)) | ||
return handler->isSideEffecting(op); | ||
|
||
return SideEffecting::Always; | ||
} | ||
}; | ||
|
||
} // namespace mlir | ||
|
||
#endif // MLIR_TRANSFORMS_SIDEEFFECTSINTERFACE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.