blob: dc27bbc1e4bb1e3787dee491410900c9fd3ca0f6 [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
23namespace clspv {
24
David Neto862b7d82018-06-14 18:48:37 -040025enum class ArgKind : int {
26 Buffer,
27 Local,
28 Pod,
29 ReadOnlyImage,
30 WriteOnlyImage,
31 Sampler,
32};
33
34// Maps an LLVM type for a kernel argument to an argument kind.
35ArgKind GetArgKindForType(llvm::Type *type);
36
37// Converts an ArgKind to its string name.
38const char* GetArgKindName(ArgKind);
39
David Neto48f56a42017-10-06 16:44:25 -040040// Maps an LLVM type for a kernel argument to an argument
David Neto4feb7a42017-10-06 17:29:42 -040041// kind suitable for a descriptor map. The result is one of:
David Netoc6f3ab22018-04-06 18:02:31 -040042// buffer - storage buffer
43// local - array in Workgroup storage, number of elements given by
44// a specialization constant
45// pod - plain-old-data
46// ro_image - read-only image
47// wo_image - write-only image
48// sampler - sampler
David Neto862b7d82018-06-14 18:48:37 -040049inline const char *GetArgKindNameForType(llvm::Type *type) {
50 return GetArgKindName(GetArgKindForType(type));
51}
David Neto48f56a42017-10-06 16:44:25 -040052
David Netoc6f3ab22018-04-06 18:02:31 -040053// Returns true if the given type is a pointer-to-local type.
54bool IsLocalPtr(llvm::Type* type);
55
David Neto862b7d82018-06-14 18:48:37 -040056// Returns true if the given type is a sampler type. If it is, then the
57// struct type is sent back through the ptr argument.
58bool IsSamplerType(llvm::Type *type, llvm::Type **struct_type_ptr = nullptr);
59
60// Returns true if the given type is a image type. If it is, then the
61// struct type is sent back through the ptr argument.
62bool IsImageType(llvm::Type *type, llvm::Type **struct_type_ptr = nullptr);
63
David Netoc6f3ab22018-04-06 18:02:31 -040064using ArgIdMapType = llvm::DenseMap<const llvm::Argument*, int>;
65
66// Returns a mapping from pointer-to-local Argument to a specialization constant
67// ID for that argument's array size. The lowest value allocated is 3.
68//
69// The mapping is as follows:
70// - The first index used is 3.
71// - There are no gaps in the list of used indices.
72// - Arguments from earlier kernel bodies have lower indices than arguments from
73// later kernel bodies.
74// - Lower-numbered arguments have lower indices than higher-numbered arguments
75// in the same function.
76// Note that this mapping is stable as long as the order of kernel bodies is
77// retained, and the number and order of pointer-to-local arguments is retained.
78ArgIdMapType AllocateArgSpecIds(llvm::Module &M);
79
David Neto48f56a42017-10-06 16:44:25 -040080} // namespace clspv
81
82#endif