Skip to content

include/tenzr/txpr/emitter.h

Namespaces

Name
tenzr
tenzr::txpr

Classes

Name
struct tenzr::txpr::TxprCompiledModule
A lowered and compiled Txpr module pairing the emitted MLIR text with its input and output variable descriptors.
class tenzr::txpr::TxprEmitter
Lowers a Txpr into StableHLO MLIR text by assigning each variable a unique SSA name.

Source code

// Copyright 2026 The Tenzr Project Authors
//
// SPDX-License-Identifier: Apache-2.0
//
// 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
//
//      https://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 TENZR_TXPR_EMITTER_H_
#define TENZR_TXPR_EMITTER_H_

#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

#include "tenzr/frontend/dtype.h"
#include "tenzr/frontend/shape.h"
#include "tenzr/txpr/txpr.h"

namespace tenzr::txpr {

  struct TxprCompiledModule {
    std::string mlir_text;
    std::vector<VarId> in_vars;
    std::vector<VarId> out_vars;
    std::vector<Shape> in_shapes;
    std::vector<DType> in_dtypes;
    std::vector<Shape> out_shapes;
    std::vector<DType> out_dtypes;
  };

  using TapeCompiledModule = TxprCompiledModule;

  class TxprEmitter {
   public:
    TxprEmitter() = default;

    TxprCompiledModule Emit(const Txpr& txpr);

   private:
    [[nodiscard]] std::string NextSsa();
    [[nodiscard]] std::string GetVarSsa(VarId var) const;
    [[nodiscard]] std::string EnsureBroadcast(VarId var, const Shape& target_shape, const Txpr& txpr);

    std::unordered_map<uint32_t, std::string> var_ssas_;
    std::ostringstream body_;
    size_t ssa_counter_{0};
  };

  using TapeEmitter = TxprEmitter;

}  // namespace tenzr::txpr

namespace tenzr {
  using txpr::TapeCompiledModule;
  using txpr::TapeEmitter;
  using txpr::TxprCompiledModule;
  using txpr::TxprEmitter;
}  // namespace tenzr

#endif  // TENZR_TXPR_EMITTER_H_