Line data Source code
1 : /**
2 : * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef CCU_MICROCODE_OPT_MICROCODE_OPTIMIZER_H
12 : #define CCU_MICROCODE_OPT_MICROCODE_OPTIMIZER_H
13 :
14 : #include <vector>
15 :
16 : #include "ccu_instr_info_v1.h"
17 : #include "instruction_scheduler.h"
18 :
19 : namespace hcomm {
20 : namespace CcuOpt {
21 :
22 : struct OptimizerStats {
23 : SchedulerStats sched{};
24 : // 后端优化耗时 (单位: 微秒 us), 由 Optimize() 填充.
25 : uint64_t pass1DurationUs = 0;
26 : uint64_t totalDurationUs = 0;
27 : };
28 :
29 : // 极简后端优化只有 CkeOnly 一档, 无寄存器重排; 保留结构体仅为对外形态统一.
30 : struct OptimizerOptions {
31 : SchedLevel schedLevel = SchedLevel::CkeOnly;
32 : };
33 :
34 : class MicrocodeOptimizer {
35 : public:
36 : MicrocodeOptimizer();
37 :
38 : // 档位设置 (schedLevel 会同步到 InstructionSchedulerOptions::level).
39 17 : void SetOptions(const OptimizerOptions& opts)
40 : {
41 17 : opts_ = opts;
42 17 : schedOpts_.level = opts.schedLevel;
43 17 : }
44 : const OptimizerOptions& Options() const { return opts_; }
45 :
46 : CcuRep::CcuInstrInfo Optimize(const CcuRep::CcuInstrInfo& input);
47 :
48 : const OptimizerStats& Stats() const { return stats_; }
49 :
50 : // 编译期默认档位: 恒为 CkeOnly (极简后端优化无其他档位).
51 : static OptimizerOptions DefaultOptions();
52 :
53 : // 后端优化统一入口: V2 场景无条件启用 (V1 由调用方保证不进来).
54 : // reserveXnId / reserveCkeId 为 translator 保留寄存器, CkeOnly 不重命名寄存器,
55 : // 故这两个参数当前仅用于日志观测.
56 : static CcuRep::CcuInstrInfo Run(const CcuRep::CcuInstrInfo& input, uint16_t reserveXnId, uint16_t reserveCkeId);
57 :
58 : private:
59 : // dfx / opt_log: 把"优化前指令序列 + 优化后指令序列 + 后→前指令下标映射"一次性写入
60 : // HCCL_INFO, 供用户结合硬件 runlog 定位错误来源.
61 : void DumpOptLog(const CcuRep::CcuInstrInfo& input, const CcuRep::CcuInstrInfo& output) const;
62 :
63 : InstructionSchedulerOptions schedOpts_;
64 : OptimizerOptions opts_;
65 : OptimizerStats stats_{};
66 : };
67 :
68 : } // namespace CcuOpt
69 : } // namespace hcomm
70 :
71 : #endif // CCU_MICROCODE_OPT_MICROCODE_OPTIMIZER_H
|