Skip to content

Commit c99b6ec

Browse files
committed
Merge branch 'master_prm' into dev
2 parents 1538fd1 + e51543d commit c99b6ec

File tree

113 files changed

+7961
-909
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+7961
-909
lines changed

.gitattributes

Lines changed: 0 additions & 5 deletions
This file was deleted.

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ endif()
128128

129129
### VKGC build LLPC ################################################################
130130
if(ICD_BUILD_LLPC)
131+
include("cmake/compilerutils.cmake")
132+
add_compilerutils_projects()
133+
131134
target_include_directories(vkgc
132135
INTERFACE
133136
${PROJECT_SOURCE_DIR}/llpc/include

cmake/compilerutils.cmake

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
##
2+
#######################################################################################################################
3+
#
4+
# Copyright (c) 2023 Advanced Micro Devices, Inc. All Rights Reserved.
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
#
24+
#######################################################################################################################
25+
26+
set(LLPC_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/..")
27+
28+
# Function to add compilerutils as LLVM external projects.
29+
# This appends the project names to LLVM_EXTERNAL_PROJECTS and sets each LLVM_EXTERNAL_*_SOURCE_DIR,
30+
# all in the caller's scope.
31+
macro(add_compilerutils_projects)
32+
if (NOT compilerutils IN_LIST LLVM_EXTERNAL_PROJECTS)
33+
if (NOT llvm_dialects IN_LIST LLVM_EXTERNAL_PROJECTS)
34+
list(APPEND LLVM_EXTERNAL_PROJECTS llvm_dialects)
35+
set(LLVM_EXTERNAL_LLVM_DIALECTS_SOURCE_DIR "${LLPC_SOURCE_DIR}/imported/llvm-dialects")
36+
endif()
37+
list(APPEND LLVM_EXTERNAL_PROJECTS CompilerUtils)
38+
set(LLVM_EXTERNAL_COMPILERUTILS_SOURCE_DIR "${LLPC_SOURCE_DIR}/compilerutils")
39+
endif()
40+
endmacro()

cmake/continuations.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525

2626
set(LLPC_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/..")
2727

28+
include("${LLPC_SOURCE_DIR}/cmake/compilerutils.cmake")
29+
2830
# Macro to add continuations and its dependencies as LLVM external projects.
2931
# This appends the project names to LLVM_EXTERNAL_PROJECTS and sets each LLVM_EXTERNAL_*_SOURCE_DIR,
3032
# all in the caller's scope.
3133
macro(add_continuations_projects)
34+
add_compilerutils_projects()
3235
if (NOT continuations IN_LIST LLVM_EXTERNAL_PROJECTS)
3336
if (NOT llvm_dialects IN_LIST LLVM_EXTERNAL_PROJECTS)
3437
list(APPEND LLVM_EXTERNAL_PROJECTS llvm_dialects)

compilerutils/CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
cmake_minimum_required(VERSION 3.13.4)
2+
3+
project(CompilerUtils LANGUAGES CXX)
4+
5+
function(set_compiler_options PROJECT_NAME)
6+
# Output with color if in terminal: https://github.com/ninja-build/ninja/wiki/FAQ
7+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
8+
target_compile_options("${PROJECT_NAME}" PRIVATE -fdiagnostics-color=always)
9+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
10+
target_compile_options("${PROJECT_NAME}" PRIVATE -fcolor-diagnostics)
11+
endif()
12+
endfunction()
13+
14+
add_llvm_library(LLVMCompilerUtils
15+
lib/CompilerUtils.cpp
16+
lib/TypeLowering.cpp
17+
18+
DEPENDS
19+
intrinsics_gen
20+
21+
LINK_COMPONENTS
22+
Analysis
23+
Core
24+
Support
25+
)
26+
27+
target_include_directories(LLVMCompilerUtils PUBLIC
28+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
29+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
30+
$<INSTALL_INTERFACE:include>
31+
)
32+
33+
target_link_libraries(LLVMCompilerUtils PUBLIC llvm_dialects)
34+
set_compiler_options(LLVMCompilerUtils)
35+
36+
target_compile_features(LLVMCompilerUtils PUBLIC cxx_std_17)
37+
set_target_properties(LLVMCompilerUtils PROPERTIES CXX_EXTENSIONS OFF)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
***********************************************************************************************************************
3+
*
4+
* Copyright (c) 2023 Advanced Micro Devices, Inc. All Rights Reserved.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
**********************************************************************************************************************/
25+
26+
//===- CompilerUtils.h - Library for compiler frontends -------------------===//
27+
//
28+
// Implements several shared helper functions.
29+
//
30+
//===----------------------------------------------------------------------===//
31+
32+
#ifndef COMPILERUTILS_H
33+
#define COMPILERUTILS_H
34+
35+
#include "llvm/ADT/ArrayRef.h"
36+
#include "llvm/ADT/StringRef.h"
37+
#include "llvm/ADT/Twine.h"
38+
#include "llvm/IR/Attributes.h"
39+
#include "llvm/IR/IRBuilder.h"
40+
41+
namespace llvm {
42+
43+
class CallInst;
44+
class Function;
45+
class Type;
46+
class Value;
47+
48+
} // namespace llvm
49+
50+
namespace CompilerUtils {
51+
52+
// Create an LLVM function call to the named function. The callee is built
53+
// automatically based on return type and its parameters.
54+
//
55+
// @param funcName : Name of the callee
56+
// @param retTy : Return type of the callee
57+
// @param args : Arguments to pass to the callee
58+
// @param attribs : Function attributes
59+
// @param instName : Name to give instruction
60+
llvm::CallInst *createNamedCall(llvm::IRBuilder<> &, llvm::StringRef, llvm::Type *, llvm::ArrayRef<llvm::Value *>,
61+
llvm::ArrayRef<llvm::Attribute::AttrKind>, const llvm::Twine & = "");
62+
63+
// Modify the function argument types, and return the new function. NOTE: the
64+
// function does not do any uses replacement, so the caller should call
65+
// replaceAllUsesWith() for the function and arguments afterwards.
66+
llvm::Function *mutateFunctionArguments(llvm::Function &, llvm::Type *, const llvm::ArrayRef<llvm::Type *>,
67+
llvm::AttributeList);
68+
69+
} // namespace CompilerUtils
70+
71+
#endif

lgc/include/lgc/util/TypeLowering.h renamed to compilerutils/include/compilerutils/TypeLowering.h

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -60,43 +60,49 @@
6060
#include "llvm/ADT/SmallVector.h"
6161
#include "llvm/IR/IRBuilder.h"
6262

63-
namespace lgc {
64-
6563
class TypeLowering;
6664

6765
/// Given a type, check if it should be replaced.
6866
///
69-
/// Return an empty vector if this function doesn't know how to handle the given type. Subsequent conversion rules will
70-
/// then be considered.
67+
/// Return an empty vector if this function doesn't know how to handle the given
68+
/// type. Subsequent conversion rules will then be considered.
7169
///
72-
/// Otherwise, return a vector with the replacement type(s). If the type is known to remain unchanged, return a
73-
/// singleton vector containing just the original type.
70+
/// Otherwise, return a vector with the replacement type(s). If the type is
71+
/// known to remain unchanged, return a singleton vector containing just the
72+
/// original type.
7473
using TypeLoweringFn = llvm::SmallVector<llvm::Type *>(TypeLowering &, llvm::Type *);
7574

76-
/// Given a constant that is known to be meant to be replaced based on its type, attempt to replace it.
75+
/// Given a constant that is known to be meant to be replaced based on its type,
76+
/// attempt to replace it.
7777
///
7878
/// Return a non-empty vector if this function was able to handle the constant.
7979
///
80-
/// Otherwise, return an empty vector, and subsequent rules will be applied. Default rules exist for poison, undef,
81-
/// and "null-like" (zeroinitializer etc.).
80+
/// Otherwise, return an empty vector, and subsequent rules will be applied.
81+
/// Default rules exist for poison, undef, and "null-like" (zeroinitializer
82+
/// etc.).
8283
using ConstantTypeLoweringFn = llvm::SmallVector<llvm::Constant *>(TypeLowering &, llvm::Constant *,
8384
llvm::ArrayRef<llvm::Type *>);
8485

8586
// =====================================================================================================================
86-
/// Helper for lowerings that need to replace values of one type by one or more values of another type.
87+
/// Helper for lowerings that need to replace values of one type by one or more
88+
/// values of another type.
8789
///
8890
/// This helper really has two parts:
8991
///
90-
/// - A type-level part that applies @ref TypeLoweringFn rules and caches the result
91-
/// - A value-level part that maintains a mapping of replaced values and provides generic handlers for core
92+
/// - A type-level part that applies @ref TypeLoweringFn rules and caches the
93+
/// result
94+
/// - A value-level part that maintains a mapping of replaced values and
95+
/// provides generic handlers for core
9296
/// instructions like phi, select, and alloca
9397
///
94-
/// The type-level part can be reused even as the value-level part is cleared by @ref finishCleanup, assuming that the
95-
/// type replacements are consistent (which they might not always be, e.g. where the replacement depends on the target
96-
/// architecture).
98+
/// The type-level part can be reused even as the value-level part is cleared by
99+
/// @ref finishCleanup, assuming that the type replacements are consistent
100+
/// (which they might not always be, e.g. where the replacement depends on the
101+
/// target architecture).
97102
///
98-
/// The value-level part is meant to be used as a nested @ref llvm_dialects::Visitor client. It requires RPO traversal
99-
/// order. Its intended use is along the following lines:
103+
/// The value-level part is meant to be used as a nested @ref
104+
/// llvm_dialects::Visitor client. It requires RPO traversal order. Its intended
105+
/// use is along the following lines:
100106
/// @code
101107
/// struct MyPayload {
102108
/// TypeLowering lowering;
@@ -108,8 +114,9 @@ using ConstantTypeLoweringFn = llvm::SmallVector<llvm::Constant *>(TypeLowering
108114
///
109115
/// MyPayload payload;
110116
///
111-
/// // Reverse post order traversal through functions, replacing instructions with converted types as we go.
112-
/// static const auto visitor = VisitorBuilder<MyPayload>
117+
/// // Reverse post order traversal through functions, replacing instructions
118+
/// with converted types as we go. static const auto visitor =
119+
/// VisitorBuilder<MyPayload>
113120
/// .add(...)
114121
/// .nest(&TypeLowering::registerVisitors)
115122
/// .build();
@@ -118,42 +125,42 @@ using ConstantTypeLoweringFn = llvm::SmallVector<llvm::Constant *>(TypeLowering
118125
/// // Fixup phi nodes.
119126
/// payload.lowering.finishPhis();
120127
///
121-
/// // Erase all instructions that "have been replaced" (by calling replaceInstruction for them).
122-
/// payload.lowering.finishCleanup();
128+
/// // Erase all instructions that "have been replaced" (by calling
129+
/// replaceInstruction for them). payload.lowering.finishCleanup();
123130
/// @endcode
124131
class TypeLowering {
125132
public:
126-
TypeLowering(llvm::LLVMContext &context);
133+
TypeLowering(llvm::LLVMContext &);
127134

128135
llvm::LLVMContext &getContext() const { return m_builder.getContext(); }
129136

130-
void addRule(std::function<TypeLoweringFn> rule);
131-
void addConstantRule(std::function<ConstantTypeLoweringFn> rule);
137+
void addRule(std::function<TypeLoweringFn>);
138+
void addConstantRule(std::function<ConstantTypeLoweringFn>);
132139

133-
llvm::ArrayRef<llvm::Type *> convertType(llvm::Type *type);
140+
llvm::ArrayRef<llvm::Type *> convertType(llvm::Type *);
134141

135-
static void registerVisitors(llvm_dialects::VisitorBuilder<TypeLowering> &builder);
142+
static void registerVisitors(llvm_dialects::VisitorBuilder<TypeLowering> &);
136143

137-
llvm::SmallVector<llvm::Value *> getValue(llvm::Value *value);
138-
llvm::SmallVector<llvm::Value *> getValueOptional(llvm::Value *value);
139-
void replaceInstruction(llvm::Instruction *inst, llvm::ArrayRef<llvm::Value *> mapping);
140-
void eraseInstruction(llvm::Instruction *inst);
144+
llvm::SmallVector<llvm::Value *> getValue(llvm::Value *);
145+
llvm::SmallVector<llvm::Value *> getValueOptional(llvm::Value *);
146+
void replaceInstruction(llvm::Instruction *, llvm::ArrayRef<llvm::Value *>);
147+
void eraseInstruction(llvm::Instruction *);
141148

142-
llvm::Function *lowerFunctionArguments(llvm::Function &fn);
149+
llvm::Function *lowerFunctionArguments(llvm::Function &);
143150
void finishPhis();
144151
bool finishCleanup();
145152

146153
private:
147-
void recordValue(llvm::Value *value, llvm::ArrayRef<llvm::Value *> mapping);
148-
void replaceMappingWith(llvm::Value *toReplace, llvm::Value *with);
154+
void recordValue(llvm::Value *, llvm::ArrayRef<llvm::Value *>);
155+
void replaceMappingWith(llvm::Value *, llvm::Value *);
149156

150-
void visitAlloca(llvm::AllocaInst &alloca);
151-
void visitExtract(llvm::ExtractValueInst &extract);
152-
void visitInsert(llvm::InsertValueInst &insert);
153-
void visitLoad(llvm::LoadInst &load);
154-
void visitPhi(llvm::PHINode &phi);
155-
void visitSelect(llvm::SelectInst &select);
156-
void visitStore(llvm::StoreInst &store);
157+
void visitAlloca(llvm::AllocaInst &);
158+
void visitExtract(llvm::ExtractValueInst &);
159+
void visitInsert(llvm::InsertValueInst &);
160+
void visitLoad(llvm::LoadInst &);
161+
void visitPhi(llvm::PHINode &);
162+
void visitSelect(llvm::SelectInst &);
163+
void visitStore(llvm::StoreInst &);
157164

158165
/// Type conversion rules.
159166
llvm::SmallVector<std::function<TypeLoweringFn>> m_rules;
@@ -170,18 +177,18 @@ class TypeLowering {
170177
/// Map original values to type-converted values.
171178
///
172179
/// For 1-1 mappings, this stores a value pointer.
173-
/// For 1-N mappings, this stores ((index << 1) | 1), where index is the index into m_convertedValueList at which the
174-
/// converted values can be found.
180+
/// For 1-N mappings, this stores ((index << 1) | 1), where index is the index
181+
/// into m_convertedValueList at which the converted values can be found.
175182
llvm::DenseMap<llvm::Value *, uintptr_t> m_valueMap;
176183
std::vector<llvm::Value *> m_convertedValueList;
177184

178-
/// Reverse map of values that occur as type-converted values to where they occur. The vector elements are either a
179-
/// value pointer (for 1-1 mapped values) or ((index << 1) | 1), where index is the index into m_convertedValueList.
185+
/// Reverse map of values that occur as type-converted values to where they
186+
/// occur. The vector elements are either a value pointer (for 1-1 mapped
187+
/// values) or ((index << 1) | 1), where index is the index into
188+
/// m_convertedValueList.
180189
llvm::DenseMap<llvm::Value *, llvm::SmallVector<uintptr_t>> m_valueReverseMap;
181190

182191
std::vector<std::pair<llvm::PHINode *, llvm::SmallVector<llvm::PHINode *>>> m_phis;
183192
std::vector<llvm::Instruction *> m_instructionsToErase;
184-
llvm::SmallVector<llvm::Function *> m_functionToErase;
193+
llvm::SmallVector<llvm::Function *> m_functionsToErase;
185194
};
186-
187-
} // namespace lgc

0 commit comments

Comments
 (0)