blob: ed354b1dbcb7240bc4e9f36afcd52e10bc40a55a [file] [log] [blame]
#include <utility>
// Copyright (c) 2019 Google LLC
// 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
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include "source/fuzz/fact_manager.h"
#include "source/fuzz/id_use_descriptor.h"
#include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
#include "source/fuzz/transformation.h"
#include "source/opt/ir_context.h"
namespace spvtools {
namespace fuzz {
class TransformationReplaceConstantWithUniform : public Transformation {
explicit TransformationReplaceConstantWithUniform(
const protobufs::TransformationReplaceConstantWithUniform& message);
protobufs::IdUseDescriptor id_use,
protobufs::UniformBufferElementDescriptor uniform_descriptor,
uint32_t fresh_id_for_access_chain, uint32_t fresh_id_for_load);
// - |message_.fresh_id_for_access_chain| and |message_.fresh_id_for_load|
// must be distinct fresh ids.
// - |message_.uniform_descriptor| specifies a result id and a list of integer
// literal indices.
// As an example, suppose |message_.uniform_descriptor| is (18, [0, 1, 0])
// It is required that:
// - the result id (18 in our example) is the id of some uniform variable
// - the module contains an integer constant instruction corresponding to
// each of the literal indices; in our example there must thus be
// OpConstant instructions %A and %B say for each of 0 and 1
// - it is legitimate to index into the uniform variable using the
// sequence of indices; in our example this means indexing into %18
// using the sequence %A %B %A
// - the module contains a uniform pointer type corresponding to the type
// of the uniform data element obtained by following these indices
// - |message_.id_use_descriptor| identifies the use of some id %C. It is
// required that:
// - this use does indeed exist in the module
// - %C is an OpConstant
// - According to the fact manager, the uniform data element specified by
// |message_.uniform_descriptor| holds a value with the same type and
// value as %C
bool IsApplicable(opt::IRContext* context,
const FactManager& fact_manager) const override;
// - Introduces two new instructions:
// - An access chain targeting the uniform data element specified by
// |message_.uniform_descriptor|, with result id
// |message_.fresh_id_for_access_chain|
// - A load from this access chain, with id |message_.fresh_id_for_load|
// - Replaces the id use specified by |message_.id_use_descriptor| with
// |message_.fresh_id_for_load|
void Apply(opt::IRContext* context, FactManager* fact_manager) const override;
protobufs::Transformation ToMessage() const override;
// Helper method to create an access chain for the uniform element associated
// with the transformation.
std::unique_ptr<opt::Instruction> MakeAccessChainInstruction(
spvtools::opt::IRContext* context, uint32_t constant_type_id) const;
// Helper to create a load instruction.
std::unique_ptr<opt::Instruction> MakeLoadInstruction(
spvtools::opt::IRContext* context, uint32_t constant_type_id) const;
protobufs::TransformationReplaceConstantWithUniform message_;
} // namespace fuzz
} // namespace spvtools