[PATCH] D130017: [HLSL] Add RWBuffer default constructor

2022-07-28 Thread Chris Bieneman via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG66eabeb65dc9: [HLSL] Add RWBuffer default constructor 
(authored by beanz).

Changed prior to commit:
  https://reviews.llvm.org/D130017?vs=445517=448409#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D130017/new/

https://reviews.llvm.org/D130017

Files:
  clang/include/clang/Basic/HLSLRuntime.h
  clang/lib/Sema/HLSLExternalSemaSource.cpp
  clang/test/AST/HLSL/RWBuffer-AST.hlsl
  clang/test/CodeGenHLSL/builtins/RWBuffer-constructor.hlsl

Index: clang/test/CodeGenHLSL/builtins/RWBuffer-constructor.hlsl
===
--- /dev/null
+++ clang/test/CodeGenHLSL/builtins/RWBuffer-constructor.hlsl
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
+
+RWBuffer Buf;
+
+// CHECK: define linkonce_odr noundef ptr @"??0?$RWBuffer@M@hlsl@@QAA@XZ"
+// CHECK-NEXT: entry:
+
+// CHECK: %[[HandleRes:[0-9]+]] = call ptr @llvm.dx.create.handle(i8 1)
+// CHECK: store ptr %[[HandleRes]], ptr %h, align 4
Index: clang/test/AST/HLSL/RWBuffer-AST.hlsl
===
--- clang/test/AST/HLSL/RWBuffer-AST.hlsl
+++ clang/test/AST/HLSL/RWBuffer-AST.hlsl
@@ -44,4 +44,4 @@
 // CHECK: TemplateArgument type 'float'
 // CHECK-NEXT: BuiltinType 0x{{[0-9A-Fa-f]+}} 'float'
 // CHECK-NEXT: FinalAttr 0x{{[0-9A-Fa-f]+}} <> Implicit final
-// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <>   implicit h 'void *'
+// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <>   implicit referenced h 'void *'
Index: clang/lib/Sema/HLSLExternalSemaSource.cpp
===
--- clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -11,13 +11,17 @@
 
 #include "clang/Sema/HLSLExternalSemaSource.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/Attr.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/Basic/AttrKinds.h"
+#include "clang/Basic/HLSLRuntime.h"
+#include "clang/Sema/Lookup.h"
 #include "clang/Sema/Sema.h"
 
 #include 
 
 using namespace clang;
+using namespace hlsl;
 
 namespace {
 
@@ -27,6 +31,7 @@
   CXXRecordDecl *Record = nullptr;
   ClassTemplateDecl *Template = nullptr;
   NamespaceDecl *HLSLNamespace = nullptr;
+  llvm::StringMap Fields;
 
   BuiltinTypeDeclBuilder(CXXRecordDecl *R) : Record(R) {
 Record->startDefinition();
@@ -93,6 +98,7 @@
 Field->setAccess(Access);
 Field->setImplicit(true);
 Record->addDecl(Field);
+Fields[Name] = Field;
 return *this;
   }
 
@@ -101,6 +107,71 @@
 return addMemberVariable("h", Record->getASTContext().VoidPtrTy, Access);
   }
 
+  static DeclRefExpr *lookupBuiltinFunction(ASTContext , Sema ,
+StringRef Name) {
+CXXScopeSpec SS;
+IdentifierInfo  = AST.Idents.get(Name, tok::TokenKind::identifier);
+DeclarationNameInfo NameInfo =
+DeclarationNameInfo(DeclarationName(), SourceLocation());
+LookupResult R(S, NameInfo, Sema::LookupOrdinaryName);
+S.LookupParsedName(R, S.getCurScope(), , false);
+assert(R.isSingleResult() &&
+   "Since this is a builtin it should always resolve!");
+auto *VD = cast(R.getFoundDecl());
+QualType Ty = VD->getType();
+return DeclRefExpr::Create(AST, NestedNameSpecifierLoc(), SourceLocation(),
+   VD, false, NameInfo, Ty, VK_PRValue);
+  }
+
+  static Expr *emitResourceClassExpr(ASTContext , ResourceClass RC) {
+return IntegerLiteral::Create(
+AST,
+llvm::APInt(AST.getIntWidth(AST.UnsignedCharTy),
+static_cast(RC)),
+AST.UnsignedCharTy, SourceLocation());
+  }
+
+  BuiltinTypeDeclBuilder (Sema ,
+  ResourceClass RC) {
+ASTContext  = Record->getASTContext();
+
+QualType ConstructorType =
+AST.getFunctionType(AST.VoidTy, {}, FunctionProtoType::ExtProtoInfo());
+
+CanQualType CanTy = Record->getTypeForDecl()->getCanonicalTypeUnqualified();
+DeclarationName Name = AST.DeclarationNames.getCXXConstructorName(CanTy);
+CXXConstructorDecl *Constructor = CXXConstructorDecl::Create(
+AST, Record, SourceLocation(),
+DeclarationNameInfo(Name, SourceLocation()), ConstructorType,
+AST.getTrivialTypeSourceInfo(ConstructorType, SourceLocation()),
+ExplicitSpecifier(), false, true, false,
+ConstexprSpecKind::Unspecified);
+
+DeclRefExpr *Fn =
+lookupBuiltinFunction(AST, S, "__builtin_hlsl_create_handle");
+
+Expr *RCExpr = emitResourceClassExpr(AST, RC);
+CallExpr *Call =
+CallExpr::Create(AST, Fn, {RCExpr}, AST.VoidPtrTy, VK_PRValue,
+ SourceLocation(), 

[PATCH] D130017: [HLSL] Add RWBuffer default constructor

2022-07-27 Thread Justin Bogner via Phabricator via cfe-commits
bogner accepted this revision.
bogner added a comment.
This revision is now accepted and ready to land.

Is it worth adding a test that calls this constructor / checks that we end up 
with a call to the builtin?




Comment at: clang/lib/Sema/HLSLExternalSemaSource.cpp:155
+AST, Fn,
+{emitResourceClassExpr(AST, RC)},
+AST.VoidPtrTy, VK_PRValue, SourceLocation(), FPOptionsOverride());

In this case it just creates a literal so it's technically fine, but in general 
seeing these "emit" functions in argument lists can lead to things like the 
generated IR being in a different order depending on the compiler. Better to 
store the Expr in a variable as a matter of style.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D130017/new/

https://reviews.llvm.org/D130017

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D130017: [HLSL] Add RWBuffer default constructor

2022-07-18 Thread Chris Bieneman via Phabricator via cfe-commits
beanz created this revision.
beanz added reviewers: aaron.ballman, Anastasia, kuhar, bogner, python3kgae.
Herald added a project: All.
beanz requested review of this revision.
Herald added a project: clang.

This fills out the default constructor for RWBuffer to assign the
handle with the result of __builtin_hlsl_create_handle which we can
then treat as a pointer to the resource data through the mid-level of
the compiler.

Depends on D130016 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130017

Files:
  clang/include/clang/Basic/HLSLRuntime.h
  clang/lib/Sema/HLSLExternalSemaSource.cpp
  clang/test/AST/HLSL/RWBuffer-AST.hlsl

Index: clang/test/AST/HLSL/RWBuffer-AST.hlsl
===
--- clang/test/AST/HLSL/RWBuffer-AST.hlsl
+++ clang/test/AST/HLSL/RWBuffer-AST.hlsl
@@ -34,4 +34,4 @@
 // CHECK: TemplateArgument type 'float'
 // CHECK-NEXT: BuiltinType 0x{{[0-9A-Fa-f]+}} 'float'
 // CHECK-NEXT: FinalAttr 0x{{[0-9A-Fa-f]+}} <> Implicit final
-// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <>   implicit h 'void *'
+// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <>   implicit referenced h 'void *'
Index: clang/lib/Sema/HLSLExternalSemaSource.cpp
===
--- clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -11,13 +11,17 @@
 
 #include "clang/Sema/HLSLExternalSemaSource.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/Attr.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/Basic/AttrKinds.h"
+#include "clang/Basic/HLSLRuntime.h"
+#include "clang/Sema/Lookup.h"
 #include "clang/Sema/Sema.h"
 
 #include 
 
 using namespace clang;
+using namespace hlsl;
 
 namespace {
 
@@ -27,6 +31,7 @@
   CXXRecordDecl *Record = nullptr;
   ClassTemplateDecl *Template = nullptr;
   NamespaceDecl *HLSLNamespace = nullptr;
+  llvm::StringMap Fields;
 
   BuiltinTypeDeclBuilder(CXXRecordDecl *R) : Record(R) {
 Record->startDefinition();
@@ -93,6 +98,7 @@
 Field->setAccess(Access);
 Field->setImplicit(true);
 Record->addDecl(Field);
+Fields[Name] = Field;
 return *this;
   }
 
@@ -101,6 +107,71 @@
 return addMemberVariable("h", Record->getASTContext().VoidPtrTy, Access);
   }
 
+  static DeclRefExpr *lookupBuiltinFunction(ASTContext , Sema ,
+StringRef Name) {
+CXXScopeSpec SS;
+IdentifierInfo  = AST.Idents.get(Name, tok::TokenKind::identifier);
+DeclarationNameInfo NameInfo =
+DeclarationNameInfo(DeclarationName(), SourceLocation());
+LookupResult R(S, NameInfo, Sema::LookupOrdinaryName);
+S.LookupParsedName(R, S.getCurScope(), , false);
+assert(R.isSingleResult() &&
+   "Since this is a builtin it should always resolve!");
+auto *VD = cast(R.getFoundDecl());
+QualType Ty = VD->getType();
+return DeclRefExpr::Create(AST, NestedNameSpecifierLoc(), SourceLocation(),
+   VD, false, NameInfo, Ty, VK_PRValue);
+  }
+
+  static Expr *emitResourceClassExpr(ASTContext , ResourceClass RC) {
+return IntegerLiteral::Create(
+AST,
+llvm::APInt(AST.getIntWidth(AST.UnsignedCharTy),
+static_cast(RC)),
+AST.UnsignedCharTy, SourceLocation());
+  }
+
+  BuiltinTypeDeclBuilder (Sema ,
+  ResourceClass RC) {
+ASTContext  = Record->getASTContext();
+
+QualType ConstructorType =
+AST.getFunctionType(AST.VoidTy, {}, FunctionProtoType::ExtProtoInfo());
+
+CanQualType CanTy = Record->getTypeForDecl()->getCanonicalTypeUnqualified();
+DeclarationName Name = AST.DeclarationNames.getCXXConstructorName(CanTy);
+CXXConstructorDecl *Constructor = CXXConstructorDecl::Create(
+AST, Record, SourceLocation(),
+DeclarationNameInfo(Name, SourceLocation()), ConstructorType,
+AST.getTrivialTypeSourceInfo(ConstructorType, SourceLocation()),
+ExplicitSpecifier(), false, true, false,
+ConstexprSpecKind::Unspecified);
+
+DeclRefExpr *Fn =
+lookupBuiltinFunction(AST, S, "__builtin_hlsl_create_handle");
+
+CallExpr *Call = CallExpr::Create(
+AST, Fn,
+{emitResourceClassExpr(AST, RC)},
+AST.VoidPtrTy, VK_PRValue, SourceLocation(), FPOptionsOverride());
+
+CXXThisExpr *This = new (AST)
+CXXThisExpr(SourceLocation(), Constructor->getThisType(), true);
+MemberExpr *Handle = MemberExpr::CreateImplicit(
+AST, This, true, Fields["h"], Fields["h"]->getType(), VK_LValue,
+OK_Ordinary);
+BinaryOperator *Assign = BinaryOperator::Create(
+AST, Handle, Call, BO_Assign, Handle->getType(), VK_LValue, OK_Ordinary,
+SourceLocation(), FPOptionsOverride());
+
+Constructor->setBody(
+CompoundStmt::Create(AST, {Assign}, FPOptionsOverride(),
+