-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "PassUtils" for common functions to avoid duplicate definitions (#…
…2547) [back-porting to gh](openxla/xla@5a07f58)
- Loading branch information
Showing
11 changed files
with
111 additions
and
96 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
4 changes: 2 additions & 2 deletions
4
stablehlo/tests/transforms/stablehlo_create_compatibility_expander.mlir
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,34 @@ | ||
/* Copyright 2024 The StableHLO Authors. All Rights Reserved. | ||
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 "stablehlo/transforms/PassUtils.h" | ||
|
||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/Location.h" | ||
#include "mlir/IR/TypeUtilities.h" | ||
#include "mlir/IR/Types.h" | ||
#include "mlir/IR/Value.h" | ||
#include "mlir/Support/LLVM.h" | ||
#include "stablehlo/dialect/ChloOps.h" | ||
|
||
namespace mlir { | ||
namespace stablehlo { | ||
|
||
Value getConstantLike(OpBuilder &b, Location loc, const APFloat &constant, | ||
Value val) { | ||
Type ty = getElementTypeOrSelf(val.getType()); | ||
return b.create<mlir::chlo::ConstantLikeOp>(loc, b.getFloatAttr(ty, constant), | ||
val); | ||
} | ||
|
||
} // namespace stablehlo | ||
} // namespace mlir |
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,57 @@ | ||
/* Copyright 2024 The StableHLO Authors. All Rights Reserved. | ||
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 THIRD_PARTY_STABLEHLO_STABLEHLO_TRANSFORMS_PASS_UTILS_H_ | ||
#define THIRD_PARTY_STABLEHLO_STABLEHLO_TRANSFORMS_PASS_UTILS_H_ | ||
|
||
#include "llvm/Support/ErrorHandling.h" | ||
#include "mlir/Dialect/Complex/IR/Complex.h" | ||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/BuiltinAttributeInterfaces.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/IR/Location.h" | ||
#include "mlir/IR/TypeUtilities.h" | ||
#include "mlir/IR/Types.h" | ||
#include "mlir/IR/Value.h" | ||
#include "mlir/Support/LLVM.h" | ||
#include "stablehlo/dialect/ChloOps.h" | ||
|
||
namespace mlir { | ||
namespace stablehlo { | ||
// Add utility functions common across passes. | ||
|
||
// Creates a chlo::ConstantLikeOp using a splat `constant` of the same shape | ||
// as `val`. | ||
template <typename T> | ||
Value getConstantLike(OpBuilder &b, Location loc, T constant, Value val) { | ||
Type ty = getElementTypeOrSelf(val.getType()); | ||
auto getAttr = [&]() -> Attribute { | ||
if (isa<IntegerType>(ty)) return b.getIntegerAttr(ty, constant); | ||
if (isa<FloatType>(ty)) return b.getFloatAttr(ty, constant); | ||
if (auto complexTy = dyn_cast<ComplexType>(ty)) { | ||
return complex::NumberAttr::get(complexTy, constant, 0); | ||
} | ||
llvm_unreachable("unhandled element type"); | ||
}; | ||
return b.create<mlir::chlo::ConstantLikeOp>(loc, cast<TypedAttr>(getAttr()), | ||
val); | ||
} | ||
|
||
// Creates a chlo::ConstantLikeOp using a APFloat splat `constant` of the | ||
// same shape as `val`. | ||
Value getConstantLike(OpBuilder &b, Location loc, const APFloat &constant, | ||
Value val); | ||
|
||
} // namespace stablehlo | ||
} // namespace mlir | ||
|
||
#endif // THIRD_PARTY_STABLEHLO_STABLEHLO_TRANSFORMS_PASS_UTILS_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
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