Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][Interp] IntegralAP zero-init #68081

Merged
merged 1 commit into from
Oct 19, 2023
Merged

Conversation

tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Oct 3, 2023

Not adding any tests since I'm waiting for #68069

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 3, 2023
@llvmbot
Copy link

llvmbot commented Oct 3, 2023

@llvm/pr-subscribers-clang

Changes

Not adding any tests since I'm waiting for #68069


Full diff: https://github.com/llvm/llvm-project/pull/68081.diff

4 Files Affected:

  • (modified) clang/lib/AST/Interp/ByteCodeExprGen.cpp (+2-2)
  • (modified) clang/lib/AST/Interp/IntegralAP.h (+5-4)
  • (modified) clang/lib/AST/Interp/Interp.h (+10)
  • (modified) clang/lib/AST/Interp/Opcodes.td (+14-1)
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index e266804a4e75dea..9a2bbe5c1841208 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1642,9 +1642,9 @@ bool ByteCodeExprGen<Emitter>::visitZeroInitializer(QualType QT,
   case PT_Uint64:
     return this->emitZeroUint64(E);
   case PT_IntAP:
+    return this->emitZeroIntAP(128, E); // FIXME: Ctx.getBitWidth()
   case PT_IntAPS:
-    assert(false);
-    return false;
+    return this->emitZeroIntAPS(128, E); // FIXME: Ctx.getBitWidth()
   case PT_Ptr:
     return this->emitNullPtr(E);
   case PT_FnPtr:
diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h
index a8df431bef11784..bca39884ac1de88 100644
--- a/clang/lib/AST/Interp/IntegralAP.h
+++ b/clang/lib/AST/Interp/IntegralAP.h
@@ -100,12 +100,13 @@ template <bool Signed> class IntegralAP final {
   }
   static IntegralAP from(const Boolean &B) {
     assert(false);
-    return IntegralAP::zero();
+    return IntegralAP::zero(1);
   }
 
-  static IntegralAP zero() {
-    assert(false);
-    return IntegralAP(0);
+  static IntegralAP zero(int32_t BitWidth) {
+    APSInt V =
+        APSInt(APInt(BitWidth, static_cast<int64_t>(0), Signed), !Signed);
+    return IntegralAP(V);
   }
 
   // FIXME: This can't be static if the bitwidth depends on V.
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 9d5ec3315415cf7..f768deca62b8b0a 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1622,6 +1622,16 @@ bool Zero(InterpState &S, CodePtr OpPC) {
   return true;
 }
 
+static inline bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
+  S.Stk.push<IntegralAP<false>>(IntegralAP<false>::zero(BitWidth));
+  return true;
+}
+
+static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
+  S.Stk.push<IntegralAP<true>>(IntegralAP<true>::zero(BitWidth));
+  return true;
+}
+
 template <PrimType Name, class T = typename PrimConv<Name>::T>
 inline bool Null(InterpState &S, CodePtr OpPC) {
   S.Stk.push<T>();
diff --git a/clang/lib/AST/Interp/Opcodes.td b/clang/lib/AST/Interp/Opcodes.td
index 9fc4938bb37bde8..d78431c56645629 100644
--- a/clang/lib/AST/Interp/Opcodes.td
+++ b/clang/lib/AST/Interp/Opcodes.td
@@ -72,6 +72,11 @@ def IntegerTypeClass : TypeClass {
                Uint32, Sint64, Uint64, IntAP, IntAPS];
 }
 
+def FixedSizeIntegralTypeClass : TypeClass {
+  let Types = [Sint8, Uint8, Sint16, Uint16, Sint32,
+               Uint32, Sint64, Uint64, Bool];
+}
+
 def NumberTypeClass : TypeClass {
   let Types = !listconcat(IntegerTypeClass.Types, [Float]);
 }
@@ -243,10 +248,18 @@ def ConstBool : ConstOpcode<Bool, ArgBool>;
 
 // [] -> [Integer]
 def Zero : Opcode {
-  let Types = [AluTypeClass];
+  let Types = [FixedSizeIntegralTypeClass];
   let HasGroup = 1;
 }
 
+def ZeroIntAP : Opcode {
+  let Args = [ArgUint32];
+}
+
+def ZeroIntAPS : Opcode {
+  let Args = [ArgUint32];
+}
+
 // [] -> [Pointer]
 def Null : Opcode {
   let Types = [PtrTypeClass];

@tbaederr
Copy link
Contributor Author

Ping

clang/lib/AST/Interp/ByteCodeExprGen.cpp Outdated Show resolved Hide resolved
return IntegralAP(0);
static IntegralAP zero(int32_t BitWidth) {
APSInt V =
APSInt(APInt(BitWidth, static_cast<int64_t>(0), Signed), !Signed);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
APSInt(APInt(BitWidth, static_cast<int64_t>(0), Signed), !Signed);
APSInt(APInt(BitWidth, 0LL, Signed), !Signed);

we don't even need the LL do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I keep having to add this cast to int64_t because some Windows builders will otherwise complain because the APInt class has two constructors it can take:

APInt (unsigned numBits, uint64_t val, bool isSigned=false)
APInt (unsigned numBits, unsigned numWords, const uint64_t bigVal[])

and MSVC doesn't know which one to take.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh wow, that's... neat. The explicit cast is also fine, but perhaps should be to uint64_t to remove one more implicit conversion step.

@@ -100,12 +100,13 @@ template <bool Signed> class IntegralAP final {
}
static IntegralAP from(const Boolean &B) {
assert(false);
return IntegralAP::zero();
return IntegralAP::zero(1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does this not look at the value of B?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is unimplemented right now, I just added the bitwidth to make it compile.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, please add FIXME comments to unimplemented stuff when you can.

(I'll be honest -- reviewing new interpreter patches is getting really difficult because of the interdependence between so many unrelated reviews and how much stuff is half-done. That's why these reviews keep getting delayed on my end, there's just too much to try to keep track of spread out across too many reviews. I wish we could find a way to improve this without impeding your ability to make progress. CC @shafik @erichkeane @cor3ntin as other folks doing a fair number of these reviews in case this is a shared concern and folks have ideas on how to improve it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have such comments because I opted for assert(false) where things are unimplemented :)

I keep opening smaller PRs to make review easier because I thought that... makes it easier. If larger (but complete) patches are better for you, I can try to do that instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed that code in 66c9915.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

Aside from nits, this LGTM.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants