1fd4e5da5Sopenharmony_ci// Copyright (c) 2016 Google Inc.
2fd4e5da5Sopenharmony_ci//
3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci//
7fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci//
9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci// limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_ci// This program demonstrates basic SPIR-V module processing using
16fd4e5da5Sopenharmony_ci// SPIRV-Tools C++ API:
17fd4e5da5Sopenharmony_ci// * Assembling
18fd4e5da5Sopenharmony_ci// * Validating
19fd4e5da5Sopenharmony_ci// * Optimizing
20fd4e5da5Sopenharmony_ci// * Disassembling
21fd4e5da5Sopenharmony_ci
22fd4e5da5Sopenharmony_ci#include <iostream>
23fd4e5da5Sopenharmony_ci#include <string>
24fd4e5da5Sopenharmony_ci#include <vector>
25fd4e5da5Sopenharmony_ci
26fd4e5da5Sopenharmony_ci#include "spirv-tools/libspirv.hpp"
27fd4e5da5Sopenharmony_ci#include "spirv-tools/optimizer.hpp"
28fd4e5da5Sopenharmony_ci
29fd4e5da5Sopenharmony_ciint main() {
30fd4e5da5Sopenharmony_ci  const std::string source =
31fd4e5da5Sopenharmony_ci      "         OpCapability Linkage "
32fd4e5da5Sopenharmony_ci      "         OpCapability Shader "
33fd4e5da5Sopenharmony_ci      "         OpMemoryModel Logical GLSL450 "
34fd4e5da5Sopenharmony_ci      "         OpSource GLSL 450 "
35fd4e5da5Sopenharmony_ci      "         OpDecorate %spec SpecId 1 "
36fd4e5da5Sopenharmony_ci      "  %int = OpTypeInt 32 1 "
37fd4e5da5Sopenharmony_ci      " %spec = OpSpecConstant %int 0 "
38fd4e5da5Sopenharmony_ci      "%const = OpConstant %int 42";
39fd4e5da5Sopenharmony_ci
40fd4e5da5Sopenharmony_ci  spvtools::SpirvTools core(SPV_ENV_UNIVERSAL_1_3);
41fd4e5da5Sopenharmony_ci  spvtools::Optimizer opt(SPV_ENV_UNIVERSAL_1_3);
42fd4e5da5Sopenharmony_ci
43fd4e5da5Sopenharmony_ci  auto print_msg_to_stderr = [](spv_message_level_t, const char*,
44fd4e5da5Sopenharmony_ci                                const spv_position_t&, const char* m) {
45fd4e5da5Sopenharmony_ci    std::cerr << "error: " << m << std::endl;
46fd4e5da5Sopenharmony_ci  };
47fd4e5da5Sopenharmony_ci  core.SetMessageConsumer(print_msg_to_stderr);
48fd4e5da5Sopenharmony_ci  opt.SetMessageConsumer(print_msg_to_stderr);
49fd4e5da5Sopenharmony_ci
50fd4e5da5Sopenharmony_ci  std::vector<uint32_t> spirv;
51fd4e5da5Sopenharmony_ci  if (!core.Assemble(source, &spirv)) return 1;
52fd4e5da5Sopenharmony_ci  if (!core.Validate(spirv)) return 1;
53fd4e5da5Sopenharmony_ci
54fd4e5da5Sopenharmony_ci  opt.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass({{1, "42"}}))
55fd4e5da5Sopenharmony_ci      .RegisterPass(spvtools::CreateFreezeSpecConstantValuePass())
56fd4e5da5Sopenharmony_ci      .RegisterPass(spvtools::CreateUnifyConstantPass())
57fd4e5da5Sopenharmony_ci      .RegisterPass(spvtools::CreateStripDebugInfoPass());
58fd4e5da5Sopenharmony_ci  if (!opt.Run(spirv.data(), spirv.size(), &spirv)) return 1;
59fd4e5da5Sopenharmony_ci
60fd4e5da5Sopenharmony_ci  std::string disassembly;
61fd4e5da5Sopenharmony_ci  if (!core.Disassemble(spirv, &disassembly)) return 1;
62fd4e5da5Sopenharmony_ci  std::cout << disassembly << "\n";
63fd4e5da5Sopenharmony_ci
64fd4e5da5Sopenharmony_ci  return 0;
65fd4e5da5Sopenharmony_ci}
66