blob: b60043659cb9c3eef355876c1f7b96d4caf9586a [file] [log] [blame]
David Neto48f56a42017-10-06 16:44:25 -04001// Copyright 2017 The Clspv Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
David Neto4feb7a42017-10-06 17:29:42 -040015#ifndef CLSPV_LIB_ARGKIND_H_
16#define CLSPV_LIB_ARGKIND_H_
David Neto48f56a42017-10-06 16:44:25 -040017
David Netoc6f3ab22018-04-06 18:02:31 -040018#include "llvm/IR/Function.h"
19#include "llvm/IR/Module.h"
20#include "llvm/IR/Type.h"
21#include "llvm/ADT/DenseMap.h"
David Neto48f56a42017-10-06 16:44:25 -040022
alan-bakerf5e5f692018-11-27 08:33:24 -050023#include "clspv/ArgKind.h"
David Neto48f56a42017-10-06 16:44:25 -040024
alan-bakerf5e5f692018-11-27 08:33:24 -050025namespace clspv {
David Neto862b7d82018-06-14 18:48:37 -040026
27// Maps an LLVM type for a kernel argument to an argument kind.
28ArgKind GetArgKindForType(llvm::Type *type);
29
David Neto48f56a42017-10-06 16:44:25 -040030// Maps an LLVM type for a kernel argument to an argument
David Neto4feb7a42017-10-06 17:29:42 -040031// kind suitable for a descriptor map. The result is one of:
Alan Bakerfcda9482018-10-02 17:09:59 -040032// buffer - storage buffer
33// buffer_ubo - uniform buffer
34// local - array in Workgroup storage, number of elements given by
35// a specialization constant
36// pod - plain-old-data
37// ro_image - read-only image
38// wo_image - write-only image
39// sampler - sampler
David Neto862b7d82018-06-14 18:48:37 -040040inline const char *GetArgKindNameForType(llvm::Type *type) {
41 return GetArgKindName(GetArgKindForType(type));
42}
David Neto48f56a42017-10-06 16:44:25 -040043
David Netoc6f3ab22018-04-06 18:02:31 -040044// Returns true if the given type is a pointer-to-local type.
45bool IsLocalPtr(llvm::Type* type);
46
David Neto862b7d82018-06-14 18:48:37 -040047// Returns true if the given type is a sampler type. If it is, then the
48// struct type is sent back through the ptr argument.
49bool IsSamplerType(llvm::Type *type, llvm::Type **struct_type_ptr = nullptr);
50
51// Returns true if the given type is a image type. If it is, then the
52// struct type is sent back through the ptr argument.
53bool IsImageType(llvm::Type *type, llvm::Type **struct_type_ptr = nullptr);
54
David Netoc6f3ab22018-04-06 18:02:31 -040055using ArgIdMapType = llvm::DenseMap<const llvm::Argument*, int>;
56
57// Returns a mapping from pointer-to-local Argument to a specialization constant
58// ID for that argument's array size. The lowest value allocated is 3.
59//
60// The mapping is as follows:
61// - The first index used is 3.
62// - There are no gaps in the list of used indices.
63// - Arguments from earlier kernel bodies have lower indices than arguments from
64// later kernel bodies.
65// - Lower-numbered arguments have lower indices than higher-numbered arguments
66// in the same function.
67// Note that this mapping is stable as long as the order of kernel bodies is
68// retained, and the number and order of pointer-to-local arguments is retained.
69ArgIdMapType AllocateArgSpecIds(llvm::Module &M);
70
David Neto48f56a42017-10-06 16:44:25 -040071} // namespace clspv
72
73#endif