* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#ifndef ASCIR_TARGET_ASC_CODEEMITTER_H
#define ASCIR_TARGET_ASC_CODEEMITTER_H
#include "ascir/Dialect/Asc/IR/Asc.h"
#include "ascir/Target/Asc/EmitNameStack.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Value.h"
#include "mlir/Support/IndentedOstream.h"
#include "llvm/ADT/ScopedHashTable.h"
#include <functional>
#include <unordered_map>
namespace mlir {
static constexpr const char* ascNamespace = "AscendC";
struct CodeEmitter {
const std::string structFieldNamePrefix = "var";
static void emitCubeFormat(raw_ostream& os, ascendc::CubeFormat format);
static void emitTPosition(raw_ostream& os, ascendc::TPosition pos);
static void emitLayoutMode(raw_ostream& os, ascendc::LayoutMode layout);
explicit CodeEmitter(raw_ostream& os);
static void emitMatmulConfig(raw_ostream& os, ascendc::MatmulConfigAttr config);
LogicalResult emitAscMatmulSimplifiedTemplate(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAttribute(Location loc, Attribute attr);
LogicalResult emitType(Location loc, Type type, bool emitAsUnsigned = false);
LogicalResult emitTypes(Location loc, ArrayRef<Type> types);
LogicalResult emitVariableDeclaration(OpResult opResult, bool trailingSemicolon);
LogicalResult emitLabel(Block& block);
LogicalResult emitAssignPrefix(Operation& op);
StringRef getOrCreateName(Block& block);
bool shouldMapToUnsigned(IntegerType::SignednessSemantics val);
LogicalResult emitOperands(Operation& op);
StringRef getOrCreateName(Value val);
void emitAddressSpace(ascendc::AddressSpace addressSpace);
LogicalResult emitAscMatmulTypeTemplate(Location loc, Type type, bool emitAsUnsigned);
struct Scope {
Scope(CodeEmitter& emitter)
: valueMapperScope(emitter.valueMapper), blockMapperScope(emitter.blockMapper), emitter(emitter)
{
emitter.nameStack.pushScope();
}
~Scope() { emitter.nameStack.popScope(); }
private:
llvm::ScopedHashTableScope<Value, std::string> valueMapperScope;
llvm::ScopedHashTableScope<Block*, std::string> blockMapperScope;
CodeEmitter& emitter;
};
bool hasValueInScope(Value val);
bool hasBlockLabel(Block& block);
raw_indented_ostream& ostream() { return os; };
private:
using ValueMapper = llvm::ScopedHashTable<Value, std::string>;
using BlockMapper = llvm::ScopedHashTable<Block*, std::string>;
using StructGlobalMapper = std::unordered_map<std::string, std::string>;
using TypeEmitFn = std::function<LogicalResult(Location, Type, bool)>;
using AttributeEmitFn = std::function<LogicalResult(Location, Attribute)>;
using EmitTypeMapper = llvm::DenseMap<TypeID, TypeEmitFn>;
using EmitAttributeMapper = llvm::DenseMap<TypeID, AttributeEmitFn>;
raw_indented_ostream os;
ValueMapper valueMapper;
BlockMapper blockMapper;
StructGlobalMapper structMapper;
EmitNameStack nameStack;
EmitTypeMapper emitTypeMapper;
EmitAttributeMapper emitAttributeMapper;
void createTypeEmitMapper();
void createAttributeEmitMapper();
LogicalResult emitIndexType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitTensorType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitEmitcPointerType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitEmitcOpaqueType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitBaseMemRefType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscTBufType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscTBufPoolType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscQueueType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscQueBindType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscFixpipeParamsType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscGlobalTensorType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscDataCopyPadExtParamsType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscLocalTensorType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscLocalMemAllocatorType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscPyStructType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscMatmulType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitAscMrgSortSrcListType(Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitIntegerType(IntegerType& iType, Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitFloatType(FloatType& fType, Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitBaseMemRefType(BaseMemRefType& pType, Location loc, Type type, bool emitAsUnsigned);
LogicalResult emitFloatAttr(Location loc, Attribute attr);
LogicalResult emitIntegerAttr(Location loc, Attribute attr);
LogicalResult emitEmitcOpaqueAttr(Location loc, Attribute attr);
LogicalResult emitSymbolRefAttr(Location loc, Attribute attr);
LogicalResult emitTypeAttr(Location loc, Attribute attr);
void printInt(const APInt& value, bool isUnsigned);
void printFloat(const APFloat& value);
};
}
#endif