Skip to content

Commit 5534f5d

Browse files
jurahultensorflower-gardener
authored andcommitted
[XLA:GPU][NFC] Use constant pointers for HloInstruction and other types.
PiperOrigin-RevId: 351696339 Change-Id: I2806212d30f31052295638058131864f658cd6cd
1 parent 2cafdaf commit 5534f5d

2 files changed

Lines changed: 60 additions & 53 deletions

File tree

‎tensorflow/compiler/mlir/xla/transforms/mhlo_to_lhlo_with_xla.cc‎

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class XlaHloToLhloPass
206206
// instruction. If `num_operands` is valid, then only the first `num_operands`
207207
// operands of the HLO instruction will be considered.
208208
Status LhloDialectEmitter::CreateOperands(
209-
HloInstruction* instr, absl::optional<xla::int64> num_operands,
209+
const HloInstruction* instr, absl::optional<xla::int64> num_operands,
210210
llvm::SmallVectorImpl<Value>& operands, size_t& num_arguments,
211211
size_t& num_results) {
212212
if (num_operands.value_or(0) > instr->operand_count())
@@ -222,7 +222,7 @@ Status LhloDialectEmitter::CreateOperands(
222222
}
223223

224224
template <typename OpType>
225-
OpType LhloDialectEmitter::CreateOpWithoutAttrs(HloInstruction* instr,
225+
OpType LhloDialectEmitter::CreateOpWithoutAttrs(const HloInstruction* instr,
226226
ValueRange operands) {
227227
Location loc = getLocation(instr);
228228
NamedAttribute attrs[] = {{Identifier::get("name", builder_.getContext()),
@@ -232,15 +232,16 @@ OpType LhloDialectEmitter::CreateOpWithoutAttrs(HloInstruction* instr,
232232

233233
template <typename OpType>
234234
StatusOr<OpType> LhloDialectEmitter::CreateOpWithoutAttrs(
235-
HloInstruction* instr, size_t& num_arguments, size_t& num_results,
235+
const HloInstruction* instr, size_t& num_arguments, size_t& num_results,
236236
absl::optional<xla::int64> num_operands) {
237237
llvm::SmallVector<Value, 4> operands;
238238
TF_RETURN_IF_ERROR(CreateOperands(instr, num_operands, operands,
239239
num_arguments, num_results));
240240
return CreateOpWithoutAttrs<OpType>(instr, operands);
241241
}
242242

243-
StatusOr<mlir::Operation*> LhloDialectEmitter::EmitOp(HloInstruction* instr) {
243+
StatusOr<mlir::Operation*> LhloDialectEmitter::EmitOp(
244+
const HloInstruction* instr) {
244245
using xla::HloOpcode;
245246
switch (instr->opcode()) {
246247
case HloOpcode::kAbs:
@@ -363,11 +364,12 @@ StatusOr<mlir::Operation*> LhloDialectEmitter::EmitOp(HloInstruction* instr) {
363364
}
364365
}
365366

366-
Status LhloDialectEmitter::DefaultAction(HloInstruction* instr) {
367+
Status LhloDialectEmitter::DefaultAction(const HloInstruction* instr) {
367368
return EmitOp(instr).status();
368369
}
369370

370-
StatusOr<lmhlo::SortOp> LhloDialectEmitter::EmitSortOp(HloInstruction* instr) {
371+
StatusOr<lmhlo::SortOp> LhloDialectEmitter::EmitSortOp(
372+
const HloInstruction* instr) {
371373
TF_ASSIGN_OR_RETURN(auto sort, CreateOpWithoutAttrs<lmhlo::SortOp>(instr));
372374
auto* sort_instr = xla::Cast<xla::HloSortInstruction>(instr);
373375
sort.dimensionAttr(builder_.getI64IntegerAttr(sort_instr->sort_dimension()));
@@ -425,7 +427,7 @@ StatusOr<Value> LhloDialectEmitter::RewriteFusionOperand(
425427
}
426428

427429
StatusOr<lmhlo::FusionOp> LhloDialectEmitter::EmitFusionOp(
428-
HloInstruction* instr) {
430+
const HloInstruction* instr) {
429431
Location loc = getLocation(instr);
430432

431433
auto* fusion_instr = xla::Cast<xla::HloFusionInstruction>(instr);
@@ -494,7 +496,7 @@ StatusOr<lmhlo::FusionOp> LhloDialectEmitter::EmitFusionOp(
494496
}
495497

496498
StatusOr<mhlo::ScatterDimensionNumbers>
497-
LhloDialectEmitter::GetScatterDimensionNumbers(HloInstruction* instr) {
499+
LhloDialectEmitter::GetScatterDimensionNumbers(const HloInstruction* instr) {
498500
auto* scatter_instr = xla::Cast<xla::HloScatterInstruction>(instr);
499501

500502
const xla::ScatterDimensionNumbers& xla_scatter_dim =
@@ -509,7 +511,7 @@ LhloDialectEmitter::GetScatterDimensionNumbers(HloInstruction* instr) {
509511
}
510512

511513
StatusOr<lmhlo::ScatterOp> LhloDialectEmitter::EmitScatterOp(
512-
HloInstruction* instr) {
514+
const HloInstruction* instr) {
513515
TF_ASSIGN_OR_RETURN(auto scatter,
514516
CreateOpWithoutAttrs<lmhlo::ScatterOp>(instr));
515517

@@ -533,7 +535,7 @@ StatusOr<lmhlo::ScatterOp> LhloDialectEmitter::EmitScatterOp(
533535
}
534536

535537
StatusOr<lmhlo::SelectAndScatterOp> LhloDialectEmitter::EmitSelectAndScatterOp(
536-
HloInstruction* instr) {
538+
const HloInstruction* instr) {
537539
TF_ASSIGN_OR_RETURN(auto select_and_scatter,
538540
CreateOpWithoutAttrs<lmhlo::SelectAndScatterOp>(instr));
539541

@@ -566,7 +568,7 @@ StatusOr<lmhlo::SelectAndScatterOp> LhloDialectEmitter::EmitSelectAndScatterOp(
566568
}
567569

568570
StatusOr<mlir::Operation*> LhloDialectEmitter::EmitCustomCallOp(
569-
HloInstruction* instr) {
571+
const HloInstruction* instr) {
570572
auto* custom_call_instr = xla::Cast<xla::HloCustomCallInstruction>(instr);
571573

572574
if (xla::gpu::IsCustomCallToCusolver(*instr)) {
@@ -601,7 +603,7 @@ StatusOr<mlir::Operation*> LhloDialectEmitter::EmitCustomCallOp(
601603
}
602604

603605
StatusOr<lmhlo_gpu::CholeskyOp> LhloDialectEmitter::EmitCholesky(
604-
HloCustomCallInstruction* custom_call) {
606+
const HloCustomCallInstruction* custom_call) {
605607
TF_ASSIGN_OR_RETURN(auto cholesky_op,
606608
CreateOpWithoutAttrs<lmhlo_gpu::CholeskyOp>(custom_call));
607609
TF_ASSIGN_OR_RETURN(xla::CholeskyOptions options,
@@ -611,7 +613,7 @@ StatusOr<lmhlo_gpu::CholeskyOp> LhloDialectEmitter::EmitCholesky(
611613
}
612614

613615
StatusOr<Operation*> LhloDialectEmitter::EmitGemm(
614-
HloCustomCallInstruction* custom_call) {
616+
const HloCustomCallInstruction* custom_call) {
615617
TF_ASSIGN_OR_RETURN(
616618
auto const config,
617619
custom_call->backend_config<xla::gpu::GemmBackendConfig>());
@@ -675,7 +677,7 @@ static StatusOr<mlir::lmhlo_gpu::Activation> GetLHLOActivation(
675677
}
676678

677679
StatusOr<Operation*> LhloDialectEmitter::EmitDnnConvolution(
678-
HloCustomCallInstruction* custom_call) {
680+
const HloCustomCallInstruction* custom_call) {
679681
TF_ASSIGN_OR_RETURN(
680682
auto const backend_config,
681683
custom_call->backend_config<xla::gpu::CudnnConvBackendConfig>());
@@ -796,7 +798,7 @@ StatusOr<Operation*> LhloDialectEmitter::EmitDnnConvolution(
796798
}
797799

798800
StatusOr<Operation*> LhloDialectEmitter::EmitDnnBatchNorm(
799-
HloCustomCallInstruction* custom_call) {
801+
const HloCustomCallInstruction* custom_call) {
800802
const xla::int64 num_operands = custom_call->operand_count();
801803
auto set_batchnorm_attributes = [&](auto op) -> StatusOr<Operation*> {
802804
// The last 2 operands of a custom call for batch norm are the epsilon and
@@ -895,7 +897,7 @@ StatusOr<mlir::GetGlobalMemrefOp> LhloDialectEmitter::EmitConstant(
895897
}
896898

897899
StatusOr<lmhlo::ReduceOp> LhloDialectEmitter::EmitReduceOp(
898-
HloInstruction* instr) {
900+
const HloInstruction* instr) {
899901
TF_ASSIGN_OR_RETURN(auto reduce_op,
900902
CreateOpWithoutAttrs<lmhlo::ReduceOp>(instr));
901903
auto* reduce = xla::Cast<xla::HloReduceInstruction>(instr);
@@ -907,7 +909,8 @@ StatusOr<lmhlo::ReduceOp> LhloDialectEmitter::EmitReduceOp(
907909
return reduce_op;
908910
}
909911

910-
StatusOr<lmhlo::MapOp> LhloDialectEmitter::EmitMapOp(HloInstruction* instr) {
912+
StatusOr<lmhlo::MapOp> LhloDialectEmitter::EmitMapOp(
913+
const HloInstruction* instr) {
911914
TF_ASSIGN_OR_RETURN(auto map_op, CreateOpWithoutAttrs<lmhlo::MapOp>(instr));
912915
auto* map = xla::Cast<xla::HloMapInstruction>(instr);
913916
std::vector<int64_t> dimensions(map->dimensions().begin(),
@@ -919,7 +922,7 @@ StatusOr<lmhlo::MapOp> LhloDialectEmitter::EmitMapOp(HloInstruction* instr) {
919922
}
920923

921924
StatusOr<lmhlo::CompareOp> LhloDialectEmitter::EmitCompareOp(
922-
HloInstruction* instr) {
925+
const HloInstruction* instr) {
923926
TF_ASSIGN_OR_RETURN(auto compare_op,
924927
CreateOpWithoutAttrs<lmhlo::CompareOp>(instr));
925928

@@ -960,7 +963,7 @@ StatusOr<lmhlo::CompareOp> LhloDialectEmitter::EmitCompareOp(
960963
}
961964

962965
StatusOr<lmhlo::ReducePrecisionOp> LhloDialectEmitter::EmitReducePrecisionOp(
963-
HloInstruction* instr) {
966+
const HloInstruction* instr) {
964967
TF_ASSIGN_OR_RETURN(auto reduce_precision_op,
965968
CreateOpWithoutAttrs<lmhlo::ReducePrecisionOp>(instr));
966969
auto* reduce_precision = xla::Cast<xla::HloReducePrecisionInstruction>(instr);
@@ -972,7 +975,7 @@ StatusOr<lmhlo::ReducePrecisionOp> LhloDialectEmitter::EmitReducePrecisionOp(
972975
}
973976

974977
StatusOr<lmhlo::AllReduceOp> LhloDialectEmitter::EmitAllReduceOp(
975-
HloInstruction* instr) {
978+
const HloInstruction* instr) {
976979
TF_ASSIGN_OR_RETURN(auto all_reduce_op,
977980
CreateOpWithoutAttrs<lmhlo::AllReduceOp>(instr));
978981
auto* all_reduce = xla::Cast<xla::HloAllReduceInstruction>(instr);
@@ -993,8 +996,8 @@ StatusOr<lmhlo::AllReduceOp> LhloDialectEmitter::EmitAllReduceOp(
993996
}
994997

995998
StatusOr<lmhlo::InfeedOp> LhloDialectEmitter::EmitInfeedOp(
996-
HloInstruction* instr) {
997-
HloInfeedInstruction* infeed = xla::Cast<HloInfeedInstruction>(instr);
999+
const HloInstruction* instr) {
1000+
const HloInfeedInstruction* infeed = xla::Cast<HloInfeedInstruction>(instr);
9981001
// HLO Infeed instruction has a single operand of token type and a tuple
9991002
// with buffers and a token as its output. LMHLO Infeed operation does not
10001003
// need the token operand or result, so drop it.
@@ -1006,8 +1009,9 @@ StatusOr<lmhlo::InfeedOp> LhloDialectEmitter::EmitInfeedOp(
10061009
}
10071010

10081011
StatusOr<lmhlo::OutfeedOp> LhloDialectEmitter::EmitOutfeedOp(
1009-
HloInstruction* instr) {
1010-
HloOutfeedInstruction* outfeed = xla::Cast<HloOutfeedInstruction>(instr);
1012+
const HloInstruction* instr) {
1013+
const HloOutfeedInstruction* outfeed =
1014+
xla::Cast<HloOutfeedInstruction>(instr);
10111015
// HLO outfeed instruction has 2 operands, the source and a token, and a
10121016
// single token output. LMHLO Outfeed does not need the token operand and
10131017
// result, do drop it.
@@ -1223,7 +1227,7 @@ Status HloToLhloModule(const BufferAssignment& assignment,
12231227
module.getContext()
12241228
->loadDialect<StandardOpsDialect, mhlo::MhloDialect, lmhlo::LmhloDialect,
12251229
lmhlo_gpu::LmhloGpuDialect>();
1226-
HloComputation* computation = hlo_module.entry_computation();
1230+
const HloComputation* computation = hlo_module.entry_computation();
12271231

12281232
LhloDialectEmitter emitter(assignment, *computation, module);
12291233
TF_RETURN_IF_ERROR(emitter.Initialize());

‎tensorflow/compiler/mlir/xla/transforms/mhlo_to_lhlo_with_xla.h‎

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace mlir {
3535
// This class will process an HloModule with the supplied BufferAssignment and
3636
// populate the MLIR ModuleOp with the computation converted in the LHLO
3737
// dialect.
38-
class LhloDialectEmitter : public xla::DfsHloVisitorWithDefault {
38+
class LhloDialectEmitter : public xla::ConstDfsHloVisitorWithDefault {
3939
public:
4040
// Initializes internal data structures. It must be called before calling any
4141
// of the visitors.
@@ -49,72 +49,75 @@ class LhloDialectEmitter : public xla::DfsHloVisitorWithDefault {
4949
builder_(module.getContext()),
5050
i8_type_(builder_.getIntegerType(8)) {}
5151

52-
xla::StatusOr<mlir::Operation*> EmitOp(xla::HloInstruction* instr);
52+
xla::StatusOr<mlir::Operation*> EmitOp(const xla::HloInstruction* instr);
5353

5454
xla::StatusOr<mhlo::ScatterDimensionNumbers> GetScatterDimensionNumbers(
55-
xla::HloInstruction* instr);
55+
const xla::HloInstruction* instr);
5656

5757
private:
58-
xla::StatusOr<lmhlo::SortOp> EmitSortOp(xla::HloInstruction* instr);
59-
xla::StatusOr<lmhlo::FusionOp> EmitFusionOp(xla::HloInstruction* instr);
60-
xla::StatusOr<lmhlo::ScatterOp> EmitScatterOp(xla::HloInstruction* instr);
58+
xla::StatusOr<lmhlo::SortOp> EmitSortOp(const xla::HloInstruction* instr);
59+
xla::StatusOr<lmhlo::FusionOp> EmitFusionOp(const xla::HloInstruction* instr);
60+
xla::StatusOr<lmhlo::ScatterOp> EmitScatterOp(
61+
const xla::HloInstruction* instr);
6162
xla::StatusOr<lmhlo::SelectAndScatterOp> EmitSelectAndScatterOp(
62-
xla::HloInstruction* instr);
63+
const xla::HloInstruction* instr);
6364

64-
xla::StatusOr<Operation*> EmitCustomCallOp(xla::HloInstruction* instr);
65+
xla::StatusOr<Operation*> EmitCustomCallOp(const xla::HloInstruction* instr);
6566
xla::StatusOr<lmhlo_gpu::CholeskyOp> EmitCholesky(
66-
xla::HloCustomCallInstruction* custom_call);
67+
const xla::HloCustomCallInstruction* custom_call);
6768
xla::StatusOr<Operation*> EmitGemm(
68-
xla::HloCustomCallInstruction* custom_call);
69+
const xla::HloCustomCallInstruction* custom_call);
6970
xla::StatusOr<Operation*> EmitDnnConvolution(
70-
xla::HloCustomCallInstruction* custom_call);
71+
const xla::HloCustomCallInstruction* custom_call);
7172
xla::StatusOr<Operation*> EmitDnnBatchNorm(
72-
xla::HloCustomCallInstruction* custom_call);
73+
const xla::HloCustomCallInstruction* custom_call);
7374

74-
xla::StatusOr<lmhlo::ReduceOp> EmitReduceOp(xla::HloInstruction* instr);
75-
xla::StatusOr<GetGlobalMemrefOp> EmitConstant(xla::HloInstruction* instr) {
76-
return EmitConstant(static_cast<const xla::HloInstruction*>(instr));
77-
}
75+
xla::StatusOr<lmhlo::ReduceOp> EmitReduceOp(const xla::HloInstruction* instr);
7876
xla::StatusOr<GetGlobalMemrefOp> EmitConstant(
7977
const xla::HloInstruction* instr);
8078

81-
xla::StatusOr<lmhlo::CompareOp> EmitCompareOp(xla::HloInstruction* instr);
79+
xla::StatusOr<lmhlo::CompareOp> EmitCompareOp(
80+
const xla::HloInstruction* instr);
8281

83-
::xla::StatusOr<lmhlo::InfeedOp> EmitInfeedOp(::xla::HloInstruction* instr);
84-
::xla::StatusOr<lmhlo::OutfeedOp> EmitOutfeedOp(::xla::HloInstruction* instr);
85-
::xla::StatusOr<lmhlo::MapOp> EmitMapOp(::xla::HloInstruction* instr);
82+
xla::StatusOr<lmhlo::InfeedOp> EmitInfeedOp(const xla::HloInstruction* instr);
83+
xla::StatusOr<lmhlo::OutfeedOp> EmitOutfeedOp(
84+
const xla::HloInstruction* instr);
85+
xla::StatusOr<lmhlo::MapOp> EmitMapOp(const xla::HloInstruction* instr);
8686

8787
xla::StatusOr<lmhlo::ReducePrecisionOp> EmitReducePrecisionOp(
88-
xla::HloInstruction* instr);
88+
const xla::HloInstruction* instr);
8989

90-
xla::StatusOr<lmhlo::AllReduceOp> EmitAllReduceOp(xla::HloInstruction* instr);
90+
xla::StatusOr<lmhlo::AllReduceOp> EmitAllReduceOp(
91+
const xla::HloInstruction* instr);
9192

9293
// Create LHLO operation operands given an XLA HLO instruction. By default,
9394
// all XLA HLO operands and results are converted to MLIR and appended to
9495
// `operands`. If `num_operands` is specified, only the first `num_operand`
9596
// operands of the instruction are converted to MLIR. The function returns the
9697
// actual number of operands and results generated for MLIR in `num_arguments`
9798
// and `num_results`.
98-
xla::Status CreateOperands(xla::HloInstruction* instr,
99+
xla::Status CreateOperands(const xla::HloInstruction* instr,
99100
absl::optional<xla::int64> num_operands,
100101
SmallVectorImpl<Value>& operands,
101102
size_t& num_arguments, size_t& num_results);
102103

103104
template <typename OpType>
104105
xla::StatusOr<OpType> CreateOpWithoutAttrs(
105-
xla::HloInstruction* instr,
106+
const xla::HloInstruction* instr,
106107
absl::optional<xla::int64> num_operands = absl::nullopt) {
107108
size_t unused;
108109
return CreateOpWithoutAttrs<OpType>(instr, unused, unused, num_operands);
109110
}
110111

111112
template <typename OpType>
112113
xla::StatusOr<OpType> CreateOpWithoutAttrs(
113-
xla::HloInstruction* instr, size_t& num_arguments, size_t& num_results,
114+
const xla::HloInstruction* instr, size_t& num_arguments,
115+
size_t& num_results,
114116
absl::optional<xla::int64> num_operands = absl::nullopt);
115117

116118
template <typename OpType>
117-
OpType CreateOpWithoutAttrs(xla::HloInstruction* instr, ValueRange operands);
119+
OpType CreateOpWithoutAttrs(const xla::HloInstruction* instr,
120+
ValueRange operands);
118121

119122
template <typename T>
120123
DenseIntElementsAttr GetI64DenseElementsAttr(const T& container) {
@@ -133,11 +136,11 @@ class LhloDialectEmitter : public xla::DfsHloVisitorWithDefault {
133136
return GetI64DenseElementsAttr(elements);
134137
}
135138

136-
tensorflow::Status DefaultAction(xla::HloInstruction* instr) final;
139+
tensorflow::Status DefaultAction(const xla::HloInstruction* instr) final;
137140

138141
// Computation parameters don't need any specific handling when they are
139142
// visited, they are already processed when we enter a new computation.
140-
tensorflow::Status HandleParameter(xla::HloInstruction* instr) final {
143+
tensorflow::Status HandleParameter(const xla::HloInstruction* instr) final {
141144
return tensorflow::Status::OK();
142145
}
143146

0 commit comments

Comments
 (0)