# Copyright (c) 2021-2024 Huawei Device Co., Ltd.
# 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
#
# http://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.

module:
  name: compiler
  namespace: ark::compiler

options:
- name: compiler-non-optimizing
  type: bool
  default: false
  description: Skip all optimizations
  tags: [perf]

- name: compiler-regex
  type: std::string
  default: ".*"
  description: A regular expression that specifies methods to compile. The grammar used is ECMA-262 RegExp grammar.
  tags: [perf]

- name: compiler-visualizer-dump
  type: bool
  default: false
  description: Enable visualizer dump ir
  tags: [debug]

- name: compiler-dump
  description: Enable dump ir
  tags: [debug]
  sub_options:
  - name: final
    type: bool
    default: false
    description: Dump ir only after last (codegen) pass
  - name: compact
    type: bool
    default: false
    description: Suppress dumping of some data (bc, SaveState instructions, etc)
  - name: life-intervals
    type: bool
    default: false
    description: Dump life intervals from regalloc
  - name: folder
    type: std::string
    default: ./ir_dump
    description: Set folder for dump ir
  - name: bytecode
    type: bool
    default: false
    description: Enable printing byte code instruction after ir instruction in dump ir
  - name: peepholes
    type: bool
    default: false
    description: Enable dumps each time peephole applies
  - name: source-line
    type: bool
    default: false
    description: Dump source line after instruction

- name: compiler-disasm-dump
  description: Enable dump disasm to text files
  tags: [debug]
  sub_options:
  - name: single-file
    type: bool
    default: false
    description: Dump disasm files into single file, which is "disasm.txt"
  - name: stdout
    type: bool
    default: false
    description: Dump to the standard output
  - name: code
    type: bool
    default: true
    description: Dump generated assembly code. Default is true.
  - name: code-info
    type: bool
    default: false
    description: Dump code info
  - name: file-name
    type: std::string
    default: "disasm.txt"
    description: File name to dump. Used only when 'single-file' is set.

- name: compiler-emit-asm
  type: bool
  default: false
  description: Enable dump assembly to disasm.txt file (do not emit binary)
  tags: [debug]

- name: compiler-enable-tracing
  type: bool
  default: false
  description: Enable tracing for passes
  tags: [debug]

- name: compiler-enable-tlab-events
  type: bool
  default: false
  description: Enable events for tlab allocation
  tags: [debug]

- name: compiler-support-init-object-inst
  type: bool
  default: false
  description: Compiler don't create InitObject  and GraphChecker checks this. The options are needed for bytecode optimizer.
  tags: [test]

- name: compiler-log
  type: arg_list_t
  default:
    - none
  possible_values:
    - none
    - all
    - alias-analysis
    - balance-expr
    - branch-elim
    - checks-elim
    - codegen
    - code-sink
    - cse-opt
    - graph-cloner
    - deoptimize-elim
    - cleanup
    - if-merging
    - ifconversion
    - inlining
    - ir-builder
    - licm-opt
    - licm-cond-opt
    - loop-unswitch
    - liveness-analyzer
    - loop-transform
    - lowering
    - lse-opt
    - memory-coalescing
    - monitor-analysis
    - optimize-string-concat
    - peephole
    - rle-opt
    - regalloc
    - split-resolver
    - scheduler
    - simplify-sb
    - reserve-sb-buffer
    - savestate-opt
    - try-catch-resolving
    - vn-opt
    - bridges-ss
    - pm
    - pea
    - interop-intrinsic-opt
  description: Set log compiler components
  tags: [debug]
  delimiter: ","

- name: compiler-check-graph
  type: bool
  default: true
  description: Enable Graph checking after each optimization
  tags: [debug]

- name: compiler-check-final
  type: bool
  default: false
  description: Enable Graph checking only after last optimization(Codegen)
  tags: [debug]

- name: compiler-encode-intrinsics
  type: bool
  default: true
  description: If false, we always call C implementation of an intrinsic
  tags: [test]

- name: compiler-lowering
  type: bool
  default: true
  description: Enable Lowering Pass
  tags: [perf]

- name: compiler-code-sink
  type: bool
  default: true
  description: Enable Code Sink Pass
  tags: [perf]

- name: compiler-balance-expressions
  type: bool
  default: true
  description: Enable Balance Expressions Pass
  tags: [perf]

- name: compiler-branch-elimination
  type: bool
  default: true
  description: Enable Branch Elimination Pass
  tags: [perf]

- name: compiler-optimize-string-concat
  type: bool
  default: true
  description: Enable Optimize String.concat Pass
  tags: [perf]

- name: compiler-simplify-string-builder
  type: bool
  default: true
  description: Enable Simplify StringBuilder Pass
  tags: [perf]

- name: compiler-reserve-string-builder-buffer
  type: bool
  default: true
  description: Enable Reserve StringBuilder Buffer Pass
  tags: [perf]

- name: compiler-checks-elimination
  type: bool
  default: true
  description: Enable Checks Elimination Pass
  tags: [perf]

- name: compiler-enable-replacing-checks-on-deoptimization
  type: bool
  default: true
  description: Enable replacing checks on deoptimization in checks elimination
  tags: [perf]

- name: compiler-deoptimize-elimination
  type: bool
  default: true
  description: Enable Deoptimize Elimination Pass
  tags: [perf]

- name: compiler-save-state-elimination
  type: bool
  default: true
  description: Enable SaveState Elimination Pass
  tags: [perf]

- name: compiler-safepoint-elimination-limit
  type: uint64_t
  default: 50
  description: Set the instructions limit in deoptimize elimination for removing safe points.
  recommended_values: [10,1000]
  tags: [perf]

- name: compiler-implicit-null-check
  type: bool
  default: true
  description: Apply Implicit Null Check
  tags: [perf]

- name: compiler-licm
  type: bool
  default: true
  description: Enable LICM Pass
  tags: [perf]

- name: compiler-licm-hoist-limit
  type: uint32_t
  default: 200
  description: Set the instructions limit which are hoisted by LICM
  recommended_values: [1,200]
  tags: [perf]

- name: compiler-licm-conditions
  type: bool
  default: true
  description: Enable LICM conditions Pass
  tags: [perf]

- name: compiler-loop-unswitch
  type: bool
  default: true
  description: Enable Loop unswitch Pass
  tags: [perf]

- name: compiler-loop-unswitch-max-level
  type: uint32_t
  default: 3
  description: Max loop unswitch level
  tags: [perf]

- name: compiler-loop-unswitch-max-insts
  type: uint32_t
  default: 100
  description: Max loop unswitch instructions
  tags: [perf]

- name: compiler-loop-idioms
  type: bool
  default: true
  description: Enable Loop idioms Pass
  tags: [perf]

- name: compiler-loop-peeling
  type: bool
  default: true
  description: Enable Loop peeling Pass
  tags: [perf]

- name: compiler-loop-unroll
  type: bool
  default: true
  description: Enable Loop unroll Pass
  tags: [perf]

- name: compiler-loop-unroll-factor
  type: uint32_t
  default: 6
  description: Set loop unrolling factor
  recommended_values: [2,10]
  tags: [perf]

- name: compiler-loop-unroll-inst-limit
  type: uint32_t
  default: 100
  description: Set the instructions limit for the unrolling loop body
  recommended_values: [10,1000]
  tags: [perf]

- name: compiler-unroll-loop-with-calls
  type: bool
  default: false
  description: Enable unroll for loops containing method calls
  tags: [perf]

- name: compiler-unroll-with-side-exits
  type: bool
  default: true
  description: Enable unroll with side exits for not countable loop
  tags: [perf]

- name: compiler-unroll-unknown-trip-count
  type: bool
  default: true
  description: Enable unroll for loops for which trip count is not known in advance
  tags: [perf]

- name: compiler-lse
  type: bool
  default: true
  description: Enable Load-Store Elimination Pass
  tags: [perf]

- name: compiler-cse
  type: bool
  default: false
  description: Enable CSE Pass
  tags: [perf]

- name: compiler-vn
  type: bool
  default: true
  description: Enable Value Numbering Pass
  tags: [perf]

- name: compiler-memory-coalescing
  type: bool
  default: true
  description: Enable Memory Coalescing Pass
  tags: [perf]

- name: compiler-memory-coalescing-objects
  type: bool
  default: true
  description: Apply Memory Coalescing to objects accesses
  tags: [perf]

- name: compiler-memory-coalescing-aligned
  type: bool
  default: false
  description: Apply Memory Coalescing only to aligned accesses
  tags: [perf]

- name: compiler-print-stats
  type: bool
  default: false
  description: Print optimizations statistics
  tags: [debug]

- name: compiler-dump-stats-csv
  type: std::string
  default: ""
  description: Dump optimizations statistics in csv file
  tags: [debug]

- name: compiler-enable-ir-stats
  type: bool
  default: false
  description: Enable collecting ir stats on compiler statistic
  tags: [debug]

- name: compiler-dump-jit-stats-csv
  type: std::string
  default: ""
  description: Dump JIT compilation statistics in csv file
  tags: [debug]

- name: compiler-use-safepoint
  type: bool
  default: true
  description: Build SafePoint instructions
  tags: [test]

- name: compiler-defer-preheader-transform
  type: bool
  default: true
  description: Defer processing of loop preheader in peepholer and branch elimination until past unrolling
  tags: [perf]

- name: compiler-inlining
  type: bool
  default: true
  description: Enable inlining optimization
  tags: [perf]

- name: compiler-inline-external-methods
  type: bool
  default: true
  description: Enable inlining external methods in JIT mode
  tags: [perf]

- name: compiler-inlining-blacklist
  type: arg_list_t
  default: []
  description: Method names that should not be inlined
  delimiter: ","
  tags: [perf]

- name: compiler-inlining-max-insts
  type: uint32_t
  default: 512
  description: Maximum number of the IR instructions to be inlined, including instructions of the current graph.
  recommended_values: [50,2000]
  tags: [perf]

- name: compiler-inlining-max-depth
  type: uint32_t
  default: 12
  description: Maximum depth of the inlining.
  recommended_values: [1,20]
  tags: [perf]

- name: compiler-inlining-recursive-calls-limit
  type: uint32_t
  default: 4
  description: Maximum count of recursive calls in inlined call stack.
  recommended_values: [1,10]
  tags: [perf]

- name: compiler-inlining-insts-bc-ratio
  type: double
  default: 0.4
  description: Ratio of IR insts count to 1 byte of bytecode. Used for inlining heuristic
  recommended_values: [0.1,5]
  tags: [perf]

- name: compiler-inlining-max-bc-size
  type: uint32_t
  default: 200
  description: Maximum bytecode size for inlined functions, in bytes. Average bytecode instruction size is 2.8 bytes.
  recommended_values: [10,1000]
  tags: [perf]

- name: compiler-inlining-always-inline-bc-size
  type: uint32_t
  default: 12
  description: Always inline methods with bytecode size less or equal than this limit.
  recommended_values: [0,50]
  tags: [perf]

- name: compiler-inline-simple-only
  type: bool
  default: false
  description: Inline only simple methods that don't contain calls to runtime
  tags: [perf]

- name: compiler-inlining-skip-throw-blocks
  type: bool
  default: true
  description: Don't inline methods inside blocks throwing an exception or performing deoptimization
  tags: [perf]

- name: compiler-inlining-skip-always-throw-methods
  type: bool
  default: true
  description: Don't inline methods that always throw an exception
  tags: [perf]

- name: compiler-no-virtual-inlining
  type: bool
  default: false
  description: Disable inlining of the virtual calls
  tags: [perf]

- name: compiler-no-cha-inlining
  type: bool
  default: false
  description: Don't use CHA in the inlining
  tags: [perf]

- name: compiler-no-pic-inlining
  type: bool
  default: false
  description: Don't use Polymorphic Inline Caches in the inlining
  tags: [perf]

- name: compiler-reset-local-allocator
  type: bool
  default: true
  description: Reset local allocator between passes runs
  tags: [test]

- name: compiler-scheduling
  type: bool
  default: true
  description: Enable Scheduling Pass
  tags: [perf]

- name: compiler-sched-latency
  type: uint32_t
  default: 2
  description: Set default instruction latency
  recommended_values: [1,5]
  tags: [perf]

- name: compiler-sched-latency-long
  type: uint32_t
  default: 7
  description: Set loads latency
  recommended_values: [2,15]
  tags: [perf]

- name: compiler-if-conversion
  type: bool
  default: true
  description: Enable IfConversion Pass
  tags: [perf]

- name: compiler-if-conversion-limit
  type: uint32_t
  default: 2
  description: Maximum amount of instructions in basicblock(s) for if-conversion
  recommended_values: [1,10]
  tags: [perf]

- name: compiler-if-conversion-incrase-limit-threshold
  type: uint32_t
  default: 40
  description: If the execution of each of the edge branch is greater than threshold in percents, then the limit increases
  recommended_values: [0,100]
  tags: [perf]

- name: compiler-cross-arch
  type: std::string
  default: arm64
  possible_values:
    - arm
    - arm64
    - x86
    - x86_64
  description: On AMD64 enable creation of non-native code for testing purpose
  tags: [test]

- name: compiler-enable-events
  type: bool
  default: false
  description: Enable optimizations events dump
  tags: [debug, test]

- name: compiler-events-path
  type: std::string
  default: ./compiler/events.csv
  description: Set path for optimizations events dump
  tags: [debug]

- name: compiler-save-only-live-registers
  type: bool
  default: true
  description: Save and Load only registers that contains live values when making calls.
  tags: [perf]

- name: compiler-ignore-failures
  type: bool
  default: true
  description: Ignore failures occurred during compilation
  tags: [test]

- name: compiler-allow-backend-failures
  type: bool
  default: true
  description: Ignore failures occurred during register allocation and code generation
  tags: [test]

- name: compiler-max-gen-code-size
  type: uint32_t
  default: 66060288
  description: Maximum size of the generated code in code-cache, default 63 M
  tags: [perf]

- name: compiler-max-bytecode-size
  type: uint32_t
  default: 30000
  description: Maximum size of the method's code to be compiled.
  recommended_values: [10000,500000]
  tags: [perf]

- name: compiler-max-vregs-num
  type: uint32_t
  default: 8188
  description: Maximum number of virtual registers in a method. Can't be more than 32764.
  tags: [perf]

- name: compiler-compact-prologue
  type: bool
  default: true
  description: Omit unused callee-registers save/restore in prologue/epilogue when possible.
  tags: [perf]

- name: compiler-js-filter-intrinsics
  type: bool
  default: true
  description: Enable intrinsics filter in JS Compiler
  tags: [test]

- name: compiler-reg-acc-alloc
  type: bool
  default: true
  description: Enable accumulator register allocator pass
  tags: [test]

- name: compiler-regalloc-reg-mask
  type: uint64_t
  default: 0
  description: Mask of registers, avaliable for regalloc
  tags: [test]

- name: compiler-verify-regalloc
  type: bool
  default: true
  description: Verify register allocation results in debug mode.
  tags: [debug]

- name: compiler-move-constants
  type: bool
  default: true
  description: Enable move constants from start block closer to usage
  tags: [perf]

- name: compiler-adjust-refs
  type: bool
  default: true
  description: Enable hoisting of array data address

- name: compiler-aot-load-string-plt
  type: bool
  default: true
  description: Replace unconditional ResolveString runtime call with loading string reference from PLT slot for AOT.
  tags: [perf]

- name: compiler-remat-const
  type: bool
  default: true
  description: Rematerialize constants instead of spilling them to the stack
  tags: [perf]

- name: compiler-unfold-const-array-max-size
  type: uint32_t
  default: 20
  description: Set the maximum size of the constant array to unfold a single LoadConstArray instruction to the initial instructions set.
  recommended_values: [2,1000]
  tags: [perf]

- name: compiler-spill-fill-pair
  type: bool
  default: true
  description: Store and load values from successive stack slots as pair of values during SpillFill encoding.
  tags: [perf]

- name: compiler-cpu-features
  type: arg_list_t
  default: [sse42]
  possible_values:
    - none
    - crc32
    - sse42
    - jscvt
    - atomics
  description: Set compiler CPU features
  tags: [perf]
  delimiter: ","

- name: compiler-emit-debug-info
  type: bool
  default: false
  description: Emit DWARF debug info for JIT/AOT code, PANDA_COMPILER_DEBUG_INFO should be set
  tags: [debug]

- name: compiler-aot-ra
  type: bool
  default: true
  description: Enable AOT register allocation

- name: compiler-freq-based-branch-reorder
  type: bool
  default: true
  description: Enables/disables frequency based branch reorder

- name: compiler-freq-based-branch-reorder-threshold
  type: uint32_t
  default: 80
  description: Threshold in percents for frequency based branch reorder

- name: compiler-inline-full-intrinsics
  type: bool
  default: false
  description: Inline graph for dynamic insrinsics in IrBuilder

- name: compiler-safe-points-require-reg-map
  type: bool
  default: false
  description: Emit virtual registers stack map for safe points
  
- name: compiler-scalar-replacement
  type: bool
  default: true
  description: Enable scalar replacement optimization

- name: compiler-peepholes
  type: bool
  default: true
  description: Enable peepholes optimizations

- name: compiler-if-merging
  type: bool
  default: true
  description: Enable if-merging optimization

- name: compiler-redundant-loop-elimination
  type: bool
  default: true
  description: Enable redundant-loop-elimination optimization

- name: compiler-optimize-memory-barriers
  type: bool
  default: true
  description: Enable optimize-memory-barriers optimization

- name: compiler-profile
  type: std::string
  default: ""
  description: Path to a file with profile information

- name: compiler-force-unresolved
  type: bool
  default: false
  description: Force the compiler to generate method and field resolvers whenever it is possible (Debug mode only)
  tags: [debug]

- name: compiler-enable-fast-interop
  type: bool
  default: true
  description: Enable fast ArkTS->JS interop
  tags: [perf]

- name: compiler-interop-intrinsic-optimization
  type: bool
  default: true
  description: Enable merging local scopes in fast ArkTS->EcmaScript interop
  tags: [perf]

- name: compiler-interop-scope-object-limit
  type: uint32_t
  default: 256
  description: Object limit for merged local scope in ArkTS->EcmaScript interop
  tags: [perf]

- name: compiler-interop-try-single-scope
  type: bool
  default: false
  description: Try to create single scope for method in ArkTS->EcmaScript interop whenever possible
  tags: [perf]

- name: compiler-inst-graph-coloring-limit
  type: uint32_t
  default: 5000
  description: Maximum number of the IR instructions to be chosen for graph coloring algorithm of register allocator
  tags: [perf]

- name: compiler-enforce-safepoint-placement
  type: bool
  default: false
  description: Inserting safepoints at the beginning of every basic block and after every Nth IR instruction. N is determined by compiler-safepoint-distance-limit option
  tags: [debug]

- name: compiler-safepoint-distance-limit
  type: uint32_t
  default: 50
  description: Distance limit between inserted safepoints
  tags: [debug]

events:
- name: branch-elimination
  fields:
    - name: if_block_id
      type: uint32_t
    - name: if_block_pc
      type: uint32_t
    - name: cond_inst_id
      type: uint32_t
    - name: cond_inst_pc
      type: uint32_t
    - name: condition_type
      type: const char*
    - name: removed_edge_type
      type: bool

- name: code-sink
  fields:
    - name: sunk_inst_id
      type: uint32_t
    - name: sunk_inst_pc
      type: uint32_t
    - name: from_bb_id
      type: uint32_t
    - name: to_bb_id
      type: uint32_t
      
- name: checks-elimination
  fields:
    - name: name
      type: const char*
    - name: inst_id
      type: uint32_t
    - name: pc
      type: uint32_t

- name: cleanup
  fields:
    - name: removed_inst_id
      type: uint32_t
    - name: removed_inst_pc
      type: uint32_t

- name: cse
  fields:
    - name: removed_inst_id
      type: uint32_t
    - name: removed_inst_pc
      type: uint32_t

- name: deoptimize-elimination
  fields:
    - name: name
      type: const char*
    - name: inst_id
      type: uint32_t
    - name: pc
      type: uint32_t

- name: save-state-optimization
  fields:
    - name: name
      type: const char*
    - name: inst_id
      type: uint32_t
    - name: pc
      type: uint32_t

- name: if-conversion
  fields:
    - name: merge_block_id
      type: uint32_t
    - name: merge_block_pc
      type: uint32_t
    - name: merge_type
      type: const char*
    - name: removed_block_id1
      type: int32_t
    - name: removed_block_id2
      type: int32_t
    - name: removed_block_id3
      type: int32_t

- name: inlining
  fields:
    - name: inlined_method
      type: const char*

- name: lse
  fields:
    - name: removed_inst_id
      type: uint32_t
    - name: removed_inst_pc
      type: uint32_t
    - name: dominant_inst_id
      type: uint32_t
    - name: dominant_inst_pc
      type: uint32_t
    - name: code
      type: const char*

- name: redundant-loop-elimination
  fields:
    - name: loop_id
      type: uint32_t
    - name: loop_head_pc
      type: uint32_t

- name: loop-unroll
  fields:
    - name: loop_id
      type: uint32_t
    - name: loop_head_pc
      type: uint32_t
    - name: unroll_factor
      type: uint32_t
    - name: cloneable_inst_count
      type: uint32_t
    - name: unroll_type
      type: const char*

- name: loop-peeling
  fields:
    - name: loop_id
      type: uint32_t
    - name: loop_head_pc
      type: uint32_t
    - name: loop_exit_pc
      type: uint32_t
    - name: moved_inst_count
      type: uint32_t      

- name: memory-coalescing
  fields:
    - name: first_inst_id
      type: uint32_t
    - name: first_inst_pc
      type: uint32_t
    - name: second_inst_id
      type: uint32_t
    - name: second_inst_pc
      type: uint32_t
    - name: new_inst_id
      type: uint32_t
    - name: type
      type: const char *

- name: peephole
  fields:
    - name: name
      type: const char*
    - name: inst_id
      type: uint32_t 
    - name: pc
      type: uint32_t  
  
- name: scheduler
  fields:
    - name: block_id
      type: uint32_t
    - name: block_pc
      type: uint32_t
    - name: old_cycles
      type: uint32_t
    - name: num_barriers
      type: uint32_t
    - name: critical_path
      type: uint32_t
    - name: new_cycles
      type: uint32_t

- name: gvn
  fields:
    - name: inst_id
      type: uint32_t
    - name: inst_pc
      type: uint32_t
    - name: equal_inst_id
      type: uint32_t
    - name: equal_inst_pc
      type: uint32_t

- name: licm
  fields:
    - name: inst_id
      type: uint32_t
    - name: inst_pc
      type: uint32_t
    - name: old_loop_id
      type: uint32_t
    - name: new_loop_id
      type: uint32_t

- name: lowering
  fields:
    - name: name
      type: const char*
    - name: inst_id
      type: uint32_t
    - name: pc
      type: uint32_t

- name: reg-alloc
  fields:
    - name: sf_inst_id
      type: uint32_t
    - name: sf_purpose
      type: const char*
    - name: sf_type
      type: const char*
    - name: src
      type: uint32_t
    - name: dst
      type: uint32_t
    - name: reg_type
      type: const char*

tags:
  perf: an option that affects the performance and/or codesize
  debug: debugging option (checkers, events, dumps, logs e.t.c)
  test: a test option or an option used by other components
