1# Copyright (c) 2023-2024 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14require 'ostruct' 15require 'delegate' 16 17class Pass < SimpleDelegator 18 attr_reader :operands, :inputs 19 20 def initialize(data) 21 super(data) 22 end 23 24 def verify_type 25 possible_types = PassRegistry::pass_types 26 for pass_type in self['type'] 27 if not possible_types.include?(pass_type) 28 raise "Unknown pass-type `#{pass_type}` in pass `#{self['name']}` description." 29 end 30 end 31 self 32 end 33end 34 35module PassRegistry 36 module_function 37 38 def pass_types 39 @possible_types ||= @data.pass_types.each_pair.map { |key, value| key.to_s } 40 end 41 42 def llvm_passes 43 @passes ||= @data.llvm_passes.map do |pass| 44 Pass.new(OpenStruct.new(pass)).verify_type 45 end 46 end 47 48 def passes_namespace 49 @data.passes_namespace 50 end 51 52 def wrap_data(data) 53 @data = data 54 end 55end 56 57def Gen.on_require(data) 58 PassRegistry.wrap_data(data) 59end 60