Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ MODULE_PASS("sycl-add-opt-level-attribute", SYCLAddOptLevelAttributePass())
MODULE_PASS("compile-time-properties", CompileTimePropertiesPass())
MODULE_PASS("sycl-remangle-libspirv", SYCLRemangleLibspirvPass())
MODULE_PASS("cleanup-sycl-metadata", CleanupSYCLMetadataPass())
MODULE_PASS("cleanup-sycl-metadata-from-llvm-used", CleanupSYCLMetadataFromLLVMUsed())
MODULE_PASS("remove-device-global-from-llvm-compiler-used", RemoveDeviceGlobalFromLLVMCompilerUsed())
MODULE_PASS("sycl-create-nvvm-annotations", SYCLCreateNVVMAnnotationsPass())
MODULE_PASS("lower-slm-reservation-calls", ESIMDLowerSLMReservationCalls())
MODULE_PASS("record-sycl-aspect-names", RecordSYCLAspectNamesPass())
Expand Down
18 changes: 10 additions & 8 deletions llvm/lib/SYCLLowerIR/CleanupSYCLMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/SYCLLowerIR/CleanupSYCLMetadata.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Constants.h"
Expand All @@ -34,18 +35,18 @@ void cleanupSYCLCompilerModuleMetadata(const Module &M, llvm::StringRef MD) {
}

// GV is supposed to be either llvm.compiler.used or llvm.used.
SmallVector<Constant *>
eraseGlobalVariableAndReturnOperands(GlobalVariable *GV) {
SmallPtrSet<Constant *, 0>
eraseGlobalVariableAndReturnUniqueOperands(GlobalVariable *GV) {
assert(GV->user_empty() && "Users aren't expected");
Constant *Initializer = GV->getInitializer();
GV->setInitializer(nullptr);
GV->eraseFromParent();

// Destroy the initializer and save operands.
SmallVector<Constant *> Operands;
Operands.resize(0);
SmallPtrSet<Constant *, 0> Operands;
Operands.reserve(Initializer->getNumOperands());
for (auto &Op : Initializer->operands())
Operands.push_back(cast<Constant>(Op));
Operands.insert(cast<Constant>(Op));

assert(isSafeToDestroyConstant(Initializer) &&
"Cannot remove initializer of the given GV");
Expand Down Expand Up @@ -81,8 +82,8 @@ CleanupSYCLMetadataFromLLVMUsed::run(Module &M, ModuleAnalysisManager &) {
if (!GV)
return PreservedAnalyses::all();

SmallVector<Constant *, 8> IOperands =
eraseGlobalVariableAndReturnOperands(GV);
SmallPtrSet<Constant *, 0> IOperands =
eraseGlobalVariableAndReturnUniqueOperands(GV);
// Erase all operands.
for (auto *Op : IOperands) {
auto StrippedOp = Op->stripPointerCasts();
Expand Down Expand Up @@ -113,7 +114,8 @@ RemoveDeviceGlobalFromLLVMCompilerUsed::run(Module &M,

const auto *VAT = cast<ArrayType>(GV->getValueType());
// Destroy the initializer. Keep the operands so we keep the ones we need.
SmallVector<Constant *> IOperands = eraseGlobalVariableAndReturnOperands(GV);
SmallPtrSet<Constant *, 0> IOperands =
eraseGlobalVariableAndReturnUniqueOperands(GV);

// Iterate through all operands. If they are device_global then we drop them
// and erase them if they have no uses afterwards. All other values are kept.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
; RUN: opt --passes=cleanup-sycl-metadata-from-llvm-used %s

; Check that CleanupSYCLMetadataFromLLVMUsed pass considers the case
; when llvm.used has one value more than once. It used to perform a double free
; due to llvm::isSafeToDestroyConstant(C) for the provided @C returns true.

@C = linkonce_odr hidden addrspace(1) constant <{i64}> <{i64 0}>

@llvm.used = appending global [2 x ptr addrspace(2)] [ptr addrspace(2) addrspacecast (ptr addrspace(1) @C to ptr addrspace(2)), ptr addrspace(2) addrspacecast (ptr addrspace(1) @C to ptr addrspace(2))], section "llvm.metadata"

Loading