Skip to content

Commit

Permalink
[SYCL][PTX] Minor changes based on comments
Browse files Browse the repository at this point in the history
Signed-off-by: Steffen Larsen <[email protected]>
  • Loading branch information
Steffen Larsen committed Jun 16, 2020
1 parent 1a2575e commit 85edd46
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
30 changes: 16 additions & 14 deletions llvm/lib/Target/NVPTX/SYCL/GlobalOffset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class GlobalOffset : public ModulePass {

llvm::Function *ImplicitOffsetIntrinsic =
M.getFunction(Intrinsic::getName(Intrinsic::nvvm_implicit_offset));

if (!ImplicitOffsetIntrinsic || ImplicitOffsetIntrinsic->use_empty()) {
return false;
}

KernelImplicitArgumentType =
ArrayType::get(Type::getInt32Ty(M.getContext()), 3);
ImplicitOffsetPtrType = Type::getInt32Ty(M.getContext())->getPointerTo();
Expand All @@ -54,15 +59,11 @@ class GlobalOffset : public ModulePass {
"Intrinsic::nvvm_implicit_offset does not return the expected "
"type");

if (!ImplicitOffsetIntrinsic || ImplicitOffsetIntrinsic->use_empty()) {
return false;
}

// Find all entry points.
EntryPointMetadata = getEntryPointMetadata(M);

// Add implicit parameters to all direct and indirect users of the offset
this->addImplicitParameterToCallers(M, ImplicitOffsetIntrinsic, nullptr);
addImplicitParameterToCallers(M, ImplicitOffsetIntrinsic, nullptr);

// Assert that all uses of `ImplicitOffsetIntrinsic` are removed and delete
// it.
Expand All @@ -80,7 +81,7 @@ class GlobalOffset : public ModulePass {
LLVMContext &Ctx = M.getContext();
MDNode *FuncMetadata = EntryPointMetadata[Func];

bool AlreadyProcessed = this->ProcessedFunctions.count(Func) == 1;
bool AlreadyProcessed = ProcessedFunctions.count(Func) == 1;
if (AlreadyProcessed)
return;

Expand All @@ -89,9 +90,9 @@ class GlobalOffset : public ModulePass {
auto NvvmMetadata = M.getNamedMetadata("nvvm.annotations");
assert(NvvmMetadata && "IR compiled to PTX must have nvvm.annotations");

auto NewFunc = this->addOffsetArgumentToFunction(
M, Func, KernelImplicitArgumentType->getPointerTo(),
/*KeepOriginal=*/true)
auto NewFunc = addOffsetArgumentToFunction(
M, Func, KernelImplicitArgumentType->getPointerTo(),
/*KeepOriginal=*/true)
.first;
Argument *NewArgument = NewFunc->arg_begin() + (NewFunc->arg_size() - 1);
// Pass the values by value to the kernel
Expand All @@ -118,7 +119,7 @@ class GlobalOffset : public ModulePass {
ImplicitOffset->getAlign());
MemsetCall->addParamAttr(0, Attribute::NonNull);
MemsetCall->addDereferenceableAttr(1, AllocByteSize);
this->ProcessedFunctions[Func] = Builder.CreateConstInBoundsGEP2_32(
ProcessedFunctions[Func] = Builder.CreateConstInBoundsGEP2_32(
ImplicitOffsetType, ImplicitOffset, 0, 0);
}

Expand Down Expand Up @@ -170,13 +171,13 @@ class GlobalOffset : public ModulePass {
// Determine if `Caller` needs processed or if this is another callsite
// from an already-processed function.
Function *NewFunc;
Value *ImplicitOffset = this->ProcessedFunctions[Caller];
Value *ImplicitOffset = ProcessedFunctions[Caller];
bool AlreadyProcessed = ImplicitOffset != nullptr;
if (AlreadyProcessed) {
NewFunc = Caller;
} else {
std::tie(NewFunc, ImplicitOffset) =
this->addOffsetArgumentToFunction(M, Caller);
addOffsetArgumentToFunction(M, Caller);
}

if (!CalleeWithImplicitParam) {
Expand Down Expand Up @@ -211,7 +212,7 @@ class GlobalOffset : public ModulePass {

if (!AlreadyProcessed) {
// Process callers of the old function.
this->addImplicitParameterToCallers(M, Caller, NewFunc);
addImplicitParameterToCallers(M, Caller, NewFunc);

// Now that the old function is dead, delete it.
Caller->dropAllReferences();
Expand Down Expand Up @@ -259,6 +260,7 @@ class GlobalOffset : public ModulePass {
Func->getAddressSpace());

if (KeepOriginal) {
// TODO: Are there better naming alternatives that allow for unmangling?
NewFunc->setName(Func->getName() + "_with_offset");

ValueToValueMapTy VMap;
Expand Down Expand Up @@ -308,7 +310,7 @@ class GlobalOffset : public ModulePass {
Builder.CreateBitCast(ImplicitOffset, ImplicitOffsetPtrType);
}

this->ProcessedFunctions[NewFunc] = ImplicitOffset;
ProcessedFunctions[NewFunc] = ImplicitOffset;

// Return the new function and the offset argument.
return {NewFunc, ImplicitOffset};
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Target/NVPTX/SYCL/GlobalOffset.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===--------- GlobalOffset.cpp - Global Offset Support for CUDA --------- ===//
//===---------- GlobalOffset.h - Global Offset Support for CUDA ---------- ===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand All @@ -15,7 +15,6 @@
#ifndef LLVM_SYCL_GLOBALOFFSET_H
#define LLVM_SYCL_GLOBALOFFSET_H

#include "llvm/IR/Module.h"
#include "llvm/Pass.h"

namespace llvm {
Expand Down

0 comments on commit 85edd46

Please sign in to comment.