2016-05-22 18:17:39 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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
|
2016-05-22 18:17:39 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-05-22 18:17:39 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2016-05-22 18:17:39 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_OPT_IR_LOADER_H_
|
|
|
|
#define SOURCE_OPT_IR_LOADER_H_
|
2016-05-22 18:17:39 +00:00
|
|
|
|
|
|
|
#include <memory>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-05-22 18:17:39 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/basic_block.h"
|
|
|
|
#include "source/opt/instruction.h"
|
|
|
|
#include "source/opt/module.h"
|
2016-09-21 21:16:31 +00:00
|
|
|
#include "spirv-tools/libspirv.hpp"
|
2016-05-22 18:17:39 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
2018-07-09 15:32:29 +00:00
|
|
|
namespace opt {
|
2016-05-22 18:17:39 +00:00
|
|
|
|
|
|
|
// Loader class for constructing SPIR-V in-memory IR representation. Methods in
|
|
|
|
// this class are designed to work with the interface for spvBinaryParse() in
|
|
|
|
// libspirv.h so that we can leverage the syntax checks implemented behind it.
|
|
|
|
//
|
|
|
|
// The user is expected to call SetModuleHeader() to fill in the module's
|
|
|
|
// header, and then AddInstruction() for each decoded instruction, and finally
|
|
|
|
// EndModule() to finalize the module. The instructions processed in sequence
|
|
|
|
// by AddInstruction() should comprise a valid SPIR-V module.
|
|
|
|
class IrLoader {
|
|
|
|
public:
|
|
|
|
// Instantiates a builder to construct the given |module| gradually.
|
2016-08-20 03:17:44 +00:00
|
|
|
// All internal messages will be communicated to the outside via the given
|
|
|
|
// message |consumer|. This instance only keeps a reference to the |consumer|,
|
|
|
|
// so the |consumer| should outlive this instance.
|
2017-11-14 19:11:50 +00:00
|
|
|
IrLoader(const MessageConsumer& consumer, Module* m);
|
2016-09-21 14:53:15 +00:00
|
|
|
|
|
|
|
// Sets the source name of the module.
|
|
|
|
void SetSource(const std::string& src) { source_ = src; }
|
2016-05-22 18:17:39 +00:00
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
Module* module() const { return module_; }
|
|
|
|
|
2016-05-22 18:17:39 +00:00
|
|
|
// Sets the fields in the module's header to the given parameters.
|
|
|
|
void SetModuleHeader(uint32_t magic, uint32_t version, uint32_t generator,
|
|
|
|
uint32_t bound, uint32_t reserved) {
|
|
|
|
module_->SetHeader({magic, version, generator, bound, reserved});
|
|
|
|
}
|
2016-09-21 14:53:15 +00:00
|
|
|
// Adds an instruction to the module. Returns true if no error occurs. This
|
|
|
|
// method will properly capture and store the data provided in |inst| so that
|
|
|
|
// |inst| is no longer needed after returning.
|
|
|
|
bool AddInstruction(const spv_parsed_instruction_t* inst);
|
2016-05-22 18:17:39 +00:00
|
|
|
// Finalizes the module construction. This must be called after the module
|
2016-08-25 20:42:36 +00:00
|
|
|
// header has been set and all instructions have been added. This is
|
|
|
|
// forgiving in the case of a missing terminator instruction on a basic block,
|
|
|
|
// or a missing OpFunctionEnd. Resolves internal bookkeeping.
|
2016-05-22 18:17:39 +00:00
|
|
|
void EndModule();
|
|
|
|
|
2020-11-25 19:54:32 +00:00
|
|
|
// Sets whether extra OpLine instructions should be injected to better
|
|
|
|
// track line information.
|
|
|
|
void SetExtraLineTracking(bool flag) { extra_line_tracking_ = flag; }
|
|
|
|
|
2016-05-22 18:17:39 +00:00
|
|
|
private:
|
2016-08-20 03:17:44 +00:00
|
|
|
// Consumer for communicating messages to outside.
|
|
|
|
const MessageConsumer& consumer_;
|
2016-05-22 18:17:39 +00:00
|
|
|
// The module to be built.
|
|
|
|
Module* module_;
|
2016-09-21 14:53:15 +00:00
|
|
|
// The source name of the module.
|
|
|
|
std::string source_;
|
|
|
|
// The last used instruction index.
|
|
|
|
uint32_t inst_index_;
|
2016-05-22 18:17:39 +00:00
|
|
|
// The current Function under construction.
|
|
|
|
std::unique_ptr<Function> function_;
|
|
|
|
// The current BasicBlock under construction.
|
|
|
|
std::unique_ptr<BasicBlock> block_;
|
|
|
|
// Line related debug instructions accumulated thus far.
|
|
|
|
std::vector<Instruction> dbg_line_info_;
|
2020-11-25 19:54:32 +00:00
|
|
|
// If doing extra line tracking, this is the line instruction that should be
|
|
|
|
// applied to the next instruction. Otherwise it always contains null.
|
2020-10-29 17:06:30 +00:00
|
|
|
std::unique_ptr<Instruction> last_line_inst_;
|
2020-03-23 15:01:18 +00:00
|
|
|
|
|
|
|
// The last DebugScope information that IrLoader::AddInstruction() handled.
|
|
|
|
DebugScope last_dbg_scope_;
|
2020-11-25 19:54:32 +00:00
|
|
|
|
|
|
|
// When true, do extra line information tracking: Additional OpLine
|
|
|
|
// instructions will be injected to help track line info more robustly during
|
|
|
|
// transformations.
|
|
|
|
bool extra_line_tracking_ = true;
|
2016-05-22 18:17:39 +00:00
|
|
|
};
|
|
|
|
|
2018-07-09 15:32:29 +00:00
|
|
|
} // namespace opt
|
2016-05-22 18:17:39 +00:00
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_IR_LOADER_H_
|