image.domain\self_evolve\optimize.mbt

100% · 53 / 53 points · 110 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 优化 self_evolve/optimize
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 管道优化 — 消除冗余 + 合并相邻
  7. 7// Pipeline optimization: remove redundant + merge adjacent
  8. 8
  9. 9///|
  10. 10/// 优化管道:消除恒等算子 + 互逆算子对 + 合并相邻同类算子。
  11. 11/// 描述 Optimize pipeline: remove identity + inverse pairs + merge adjacent.
  12. 12pub fn optimize_pipeline(pipeline : @imgir.IRPipeline) -> @imgir.IRPipeline {
  13. 13● let ops = pipeline.ops
  14. 14 let result : Array[@imgir.IROp] = []
  15. 15 for op in ops {
  16. 16● if is_identity_op(op) {
  17. 17● ()
  18. 18● } else if result.length() > 0 &&
  19. 19● check_inverse_pair(result[result.length() - 1], op) {
  20. 20● ignore(result.pop())
  21. 21● } else if result.length() > 0 && can_merge(result[result.length() - 1], op) {
  22. 22● let last = result[result.length() - 1]
  23. 23● ignore(result.pop())
  24. 24● result.push(merge_ops(last, op))
  25. 25 } else {
  26. 26● result.push(op)
  27. 27 }
  28. 28 }
  29. 29● let mut pipe = @imgir.IRPipeline::new()
  30. 30 for op in result {
  31. 31● pipe = pipe.add(op)
  32. 32 }
  33. 33 pipe
  34. 34}
  35. 35
  36. 36///|
  37. 37/// 生成优化建议(不实际优化,仅报告可优化点)。
  38. 38/// 生成 Generate optimization suggestions (report only, no actual optimization).
  39. 39pub fn suggest_optimizations(
  40. 40 pipeline : @imgir.IRPipeline,
  41. 41) -> Array[OptimizationSuggestion] {
  42. 42● let ops = pipeline.ops
  43. 43 let suggestions : Array[OptimizationSuggestion] = []
  44. 44 // 检查恒等算子
  45. 45● for i in 0..<ops.length() {
  46. 46● if is_identity_op(ops[i]) {
  47. 47● suggestions.push({
  48. 48 kind: RemoveIdentity,
  49. 49 op_indices: [i],
  50. 50● description: "位置 " + i.to_string() + " 是恒等算子,可移除",
  51. 51 before_count: 1,
  52. 52 after_count: 0,
  53. 53 })
  54. 54 }
  55. 55 }
  56. 56 // 检查互逆算子对
  57. 57● for i in 0..<(ops.length() - 1) {
  58. 58● if check_inverse_pair(ops[i], ops[i + 1]) {
  59. 59● suggestions.push({
  60. 60 kind: RemoveRedundant,
  61. 61 op_indices: [i, i + 1],
  62. 62 description: "位置 " +
  63. 63● i.to_string() +
  64. 64 " 和 " +
  65. 65● (i + 1).to_string() +
  66. 66 " 互逆,可移除",
  67. 67 before_count: 2,
  68. 68 after_count: 0,
  69. 69 })
  70. 70 }
  71. 71 }
  72. 72 // 检查可合并的相邻算子
  73. 73● for i in 0..<(ops.length() - 1) {
  74. 74● if can_merge(ops[i], ops[i + 1]) {
  75. 75● suggestions.push({
  76. 76 kind: MergeAdjacent,
  77. 77 op_indices: [i, i + 1],
  78. 78 description: "位置 " +
  79. 79● i.to_string() +
  80. 80 " 和 " +
  81. 81● (i + 1).to_string() +
  82. 82 " 可合并",
  83. 83 before_count: 2,
  84. 84 after_count: 1,
  85. 85 })
  86. 86 }
  87. 87 }
  88. 88 suggestions
  89. 89}
  90. 90
  91. 91///|
  92. 92/// 完整优化:返回优化报告。
  93. 93/// 描述 Full optimization: return optimization report.
  94. 94pub fn optimize_all(pipeline : @imgir.IRPipeline) -> OptimizationReport {
  95. 95● let optimized = optimize_pipeline(pipeline)
  96. 96● let suggestions = suggest_optimizations(pipeline)
  97. 97 {
  98. 98● original_length: pipeline.length(),
  99. 99● optimized_length: optimized.length(),
  100. 100 suggestions,
  101. 101 optimized_pipeline: optimized,
  102. 102 }
  103. 103}
  104. 104
  105. 105///|
  106. 106/// 计算优化后的算子数(不实际构建管道)。
  107. 107/// 计数 Count ops after optimization (without building pipeline).
  108. 108pub fn optimized_op_count(pipeline : @imgir.IRPipeline) -> Int32 {
  109. 109● optimize_pipeline(pipeline).length()
  110. 110}