Skip to content

Enable write to global vars with HLSLVersion <= 2016 - #1526

Merged
Vishal Sharma (vcsharma) merged 15 commits into
microsoft:masterfrom
vcsharma:bugfix
Sep 8, 2018
Merged

Enable write to global vars with HLSLVersion <= 2016#1526
Vishal Sharma (vcsharma) merged 15 commits into
microsoft:masterfrom
vcsharma:bugfix

Conversation

@vcsharma

Copy link
Copy Markdown
Contributor

Enable write to global vars with HLSLVersion <= 2016

@AppVeyorBot

Copy link
Copy Markdown

@tex3d Tex Riddell (tex3d) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this to add /Gec option, and:

  • Default HLSLVersion to 2016 while emitting a warning that this is deprecated functionality.
  • Emit error if /HV is used to explicitly set version greater than 2016 (2015 is ok for non-codegen path).
  • Change the code in Parser::ParseDeclGroup that sets the global decl to const for HLSL to avoid this when compat flag is set.
  • Revert changes to SemaExpr.cpp.

@AppVeyorBot

Copy link
Copy Markdown

Vishal C Sharma added 2 commits September 5, 2018 23:21
@AppVeyorBot

Copy link
Copy Markdown

@vcsharma

Copy link
Copy Markdown
Contributor Author

Updated.

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
}

static GlobalVariable *CreateStaticGlobal(llvm::Module *M, GlobalVariable *GV) {
Constant *GC = M->getOrInsertGlobal(GV->getName().str() + "_static_copy",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use name suffix that could not be an HLSL identifier name, such as ".static.copy", since that's valid for llvm, and guaranteed to avoid collision with any HLSL name defined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
static bool CreateWriteEnabledStaticGlobals(llvm::Module *M) {
std::vector<GlobalVariable *> worklist;
for (GlobalVariable &GV : M->globals()) {
if (!GV.isConstant()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also skip GVs that are InternalLinkage to skip actual user static globals.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
}

GlobalVariable *NGV = CreateStaticGlobal(M, GV);
// insert memcpy in all entryblocks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only want to insert memcpy into entry block of main EntryFunction, not all functions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
for (User *U : GV->users()) {
if (Instruction *I = dyn_cast<Instruction>(U)) {
Function *F = I->getParent()->getParent();
if (F->getName() == "main") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main entry function is not necessarily called "main". You should pass the function in to CreateStaticGlobal() from FinishCodeGen. You can get the main function by calling GetEntryFunction() on the HLModule. And then you don't need to search for users that are in this function either, since there may be no users in main function, but you still want to insert memcpy there.

@AppVeyorBot

Copy link
Copy Markdown

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
@AppVeyorBot

Copy link
Copy Markdown

@AppVeyorBot

Copy link
Copy Markdown

@AppVeyorBot

Copy link
Copy Markdown

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
for (User *U : V->users()) {
if (isa<StoreInst>(U))
isWriteEnabled = true;
else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {

@tex3d Tex Riddell (tex3d) Sep 7, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a constant GEP expression, so best to use GEPOperator. Further, you can have multiple levels of GEP at this point, so best to recurse.

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
isWriteEnabled = true;
else
isWriteEnabled |= IsWriteEnabledGlobalRec(GU, visited);
} else isWriteEnabled |= IsWriteEnabledGlobalRec(U, visited);

@tex3d Tex Riddell (tex3d) Sep 7, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should break as soon as isWriteEnabled == true, since you don't need to search anymore. In fact, this means you don't need the local variable, since you just return true when you find a store, or when a recursive call returns true. Then return false at the end, when you didn't find a store.

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
// Returns true if any of the direct user of a global is a store inst
// otherwise recurse through the remaining users and check if any GEP
// exists and which in turn has a store inst as user.
static bool IsWriteEnabledGlobal(GlobalVariable *GV) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a better name would be GlobalHasStoreUser.

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp
@AppVeyorBot

Copy link
Copy Markdown

@AppVeyorBot

Copy link
Copy Markdown

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
}
case HLOpcodeGroup::HLCast:
case HLOpcodeGroup::HLSubscript:
isWriteEnabled |= GlobalHasStoreUserRec(U, visited);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace:

  isWriteEnabled |= GlobalHasStoreUserRec(U, visited);

with:

  if (GlobalHasStoreUserRec(U, visited))
    return true;

and return false at the end, then you can get rid of the local isWriteEnabled variable.

Vishal C Sharma added 2 commits September 7, 2018 18:39
@AppVeyorBot

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants