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_INSTRUCTION_SCHEDULER_H
12 : #define CCU_MICROCODE_OPT_INSTRUCTION_SCHEDULER_H
13 :
14 : #include <cstdint>
15 : #include <vector>
16 :
17 : #include "ccu_instr_info_v1.h"
18 : #include "extract_operands.h"
19 :
20 : namespace hcomm {
21 : namespace CcuOpt {
22 :
23 : struct SchedulerStats {
24 : uint32_t nopRemoved = 0; // 输入序列里被剥离的 NOP 数 (CkeOnly 不剥离, 恒 0)
25 : uint32_t nopInserted = 0; // 调度过程因 latency 阻塞而新插入的 NOP 数
26 : uint32_t instrReordered = 0; // 与输入顺序不同的真实指令数 (CkeOnly 不重排, 恒 0)
27 : uint32_t basicBlocks = 0; // 切分得到的 BB 个数 (CkeOnly 不切分, 占位为 1)
28 :
29 : // originIndex[i] = 调度后第 i 条指令对应的输入本地下标; -1 表示新插入的填充 NOP.
30 : std::vector<int32_t> originIndex{};
31 : std::vector<uint16_t> strippedNopIndices{}; // CkeOnly 不剥离, 恒为空.
32 : };
33 :
34 : // 指令调度算法档位. 极简后端优化只提供 CkeOnly 一档:
35 : // * CkeOnly: 默认档 (唯一档). 完全不重排 / 不剥离, 只识别 CKE 寄存器的写后读 (setcke ->
36 : // waitcke/clearcke) 并按固定 cke latency 补 NOP; XN / MS 写后读交由硬件
37 : // interlock 处理, 不补任何 NOP. 配合指令空间预留可保证优化后指令数不越界.
38 : enum class SchedLevel : uint8_t {
39 : CkeOnly = 0,
40 : };
41 :
42 : struct InstructionSchedulerOptions {
43 : SchedLevel level = SchedLevel::CkeOnly;
44 : };
45 :
46 : class InstructionScheduler {
47 : public:
48 21 : explicit InstructionScheduler(InstructionSchedulerOptions opts = {}) : opts_(opts) {}
49 :
50 : CcuRep::CcuInstrInfo Schedule(const CcuRep::CcuInstrInfo& input);
51 :
52 24 : const SchedulerStats& Stats() const { return stats_; }
53 :
54 : private:
55 : InstructionSchedulerOptions opts_;
56 : SchedulerStats stats_{};
57 :
58 : // CkeOnly (默认档) 实现: 仅顺序扫描 + 只对 CKE 写后读按固定 cke latency 补 NOP,
59 : // XN / MS 写后读不补 (硬件 interlock).
60 : CcuRep::CcuInstrInfo ScheduleCkeOnly(const CcuRep::CcuInstrInfo& input);
61 : };
62 :
63 : } // namespace CcuOpt
64 : } // namespace hcomm
65 :
66 : #endif // CCU_MICROCODE_OPT_INSTRUCTION_SCHEDULER_H
|