6.5.11 · D4 · HinglishAdvanced & Emerging Architectures

ExercisesRISC-V custom extensions for accelerators

2,850 words13 min read↑ Read in English

6.5.11 · D4 · Hardware › Advanced & Emerging Architectures › RISC-V custom extensions for accelerators

Is page mein parent note RISC-V custom extensions aur uske prerequisites assume kiye gaye hain — agar koi symbol unfamiliar lage, toh woh wahan build kiya gaya tha.

Kuch encoding problems mein 32-bit instruction word ki ye picture use hoti hai. Ise open rakhein:

Figure — RISC-V custom extensions for accelerators

Figure kaise padhein. Ek RISC-V instruction exactly 32 bits ki hoti hai, jisme 31 (sabse baayein, most significant) se lekar 0 (sabse daayein, least significant) tak bits numbered hote hain. Hardware pehle opcode dhundta hai, isliye right-to-left padhein: opcode sabse daayein ke 7 bits inst[6:0] hain. Baaki fields fixed bit ranges mein hote hain — ye exact positions yaad kar lo, kyunki neeche har hand-encoding problem inhi par depend karti hai:

Field Bit range Width Selects
funct7 inst[31:25] 7 operation
rs2 inst[24:20] 5 source register 2 (data)
rs1 inst[19:15] 5 source register 1 (data)
funct3 inst[14:12] 3 operation
rd inst[11:7] 5 destination register (data)
opcode inst[6:0] 7 which instruction family

Do chalk-blue fields (funct3, funct7) pick karte hain ki kaun sa operation chalega; teen chalk-pink fields (rs2, rs1, rd) pick karte hain ki kaun sa data use hoga.


Level 1 — Recognition

Recall Solution — L1.1

Char reserved custom opcodes hain: custom-0 = 0001011, custom-1 = 0101011, custom-2 = 1011011, custom-3 = 1111011. Answer: (b) 0001011 = custom-0. Baaki standard hain: 0110011 OP hai (register ALU / R-type), 0000011 LOAD hai, 1100011 BRANCH hai. Inhe kabhi mat chhuo — custom code ka pura point yahi hai ki woh wahan rehta hai jahan standard software kabhi emit nahi karta.

Recall Solution — L1.2

rd field (destination register), 5 bits wide, inst[11:7] occupy karta hai (upar ki table dekhein). Yeh data destination choose karta hai, operation nahi. Operation-selecting fields hain funct3 (inst[14:12]) aur funct7 (inst[31:25]).


Level 2 — Application

Recall Solution — L2.1

Operation funct3 (3 bits) aur funct7 (7 bits) se choose hota hai. Register fields data pick karte hain, operation nahi, isliye unhe count nahi karte. Answer: 1024 distinct ops per slot.

Recall Solution — L2.2

Answer: 4096.

Recall Solution — L2.3

MMIO: cycles. Custom instruction: cycles. Ratio . Answer: MMIO cycles spend karta hai, custom ; custom instruction overhead mein sasta hai. Yahi parent note ka "coupling tightness is the whole point" claim hai, numeric banaya gaya.


Level 3 — Analysis

Recall Solution — L3.1

Amdahl's Law: agar runtime ka fraction , factor se accelerate ho, toh total speedup hai Ideal (): Realistic (): Answer: zyada se zyada ; realistically . Sabak yeh hai: 40% kernel ki infinite acceleration bhi nahi todi ja sakti. Isliye pehle profile karo — ek chhoti-fraction kernel ko accelerate karna silicon ki barbaadi hai.

Recall Solution — L3.2

(A) → tightly-coupled. Operands registers mein fit hote hain, latency tiny hai, aur yeh constantly invoke hoti hai, isliye per-invocation dispatch overhead near-zero hona chahiye. rs1, rs2 ko ek naye functional unit par route karo, rd wapas likhao jaise koi bhi ALU op. (B) → loosely-coupled (RoCC). 4 MB registers mein fit nahi hota; unit ko apna memory/DMA port chahiye aur yeh hazaaron cycles chalta hai. Ek command dispatch karo, CPU ko continue karne do, baad mein result collect karo. Data volume vs latency tradeoff decide karta hai: chhota+chhota → tight, bada+streaming → loose.


Level 4 — Synthesis

Recall Solution — L4.1

Har field fill karo (figure table ke bit ranges use karke):

  • funct7 = 0000000inst[31:25]
  • rs2 = x7 = 00111inst[24:20]
  • rs1 = x6 = 00110inst[19:15]
  • funct3 = 000inst[14:12]
  • rd = x5 = 00101inst[11:7]
  • opcode = custom-0 = 0001011inst[6:0]

MSB→LSB concatenate karo (spaces sirf fields alag karte hain, word ka part nahi hain): 0000000 00111 00110 000 00101 0001011 Ek 32-bit word mein: 00000000011100110000010100001011. Ab in 32 bits ko baayein se 4-4 ke nibbles mein regroup karo: 0000 0000 0111 0011 0000 0101 0000 1011 = 0 0 7 3 0 5 0 B = 0x0073050B. Answer: 0x0073050B.

Place value se sanity check. Har field contribute karta hai (uski value) apne starting bit par left shift hokar: at bit 0, at bit 7 gives , , at bit 15 gives , at bit 20 gives , . Sum , aur . ✓

Teen-source subtlety ke baare mein. Standard R-type semantics rs1, rs2 read karte hain aur sirf rd write karte hain — encoding exactly do source register addresses deta hai. Lekin mac ko ek third source chahiye: rd ki purani value. R-type encoding mein koi teesra source named nahi hota. Toh extension ise ek microarchitectural convention se resolve karta hai: aapka custom decode logic rd field ko dono — ek read address aur ek write address (same register ka read-modify-write) — treat karta hai, register file ka ek extra read rd ka add karke. Aap koi naya bit-field nahi bana rahe — aap existing rd field ko ek dusra meaning de rahe hain sirf apni unit ke andar. (Real designs mein alternatives: ek fixed accumulator register dedicate karo, ya ek RoCC unit mein accumulator internal state ke roop mein rakho taaki koi teesra register-file port na chahiye.)

Recall Solution — L4.2

Baseline: instructions, kisi bhi ke liye valid. Accelerated, jo 4 ka multiple hai: full groups hain, har ek mein loads mac issues ka cost hai: General (edge case). likhein jahan quotient aur remainder ho. elements full mac groups mein chalte hain. Baaki bache elements 4-wide mac fill nahi kar sakte, isliye unhe ek scalar tail loop se baseline cost instructions each par finish karo:

  • Check : issues ( se match karta hai).
  • Check : issues; speedup lower clean se, kyunki tail unaccelerated hai. Jaise , tail ki fixed cost negligible ho jaati hai aur speedup ke paas aata hai. Answer: clean case ; remainder hone par tail loop ratio ko neeche kheenchta hai, bilkul ke example mein dikhaaya gaya. Gain ek issue slot ke peeche 4 μops chhupane se aata hai (SIMD width), opcode se nahi. Vector / SIMD extensions (RVV) se compare karo, jisme runtime-set vector length is haath se likhe tail-loop ki problem hataa deti hai.

Level 5 — Mastery

Recall Solution — L5.1

Amdahl: . Candidate 1 (, ): Candidate 2 (, ): Whole-program: Candidate 1 deta hai ; Candidate 2 sirf deta hai, apne flashy kernel figure ke bawajood. Per engineer-month benefit: C1 khareeda speedup / 6 months per month; C2 khareeda / 1 month per month. C1 dono — raw benefit aur cost efficiency — mein jeetta hai. Answer: pehle Candidate 1 banao. Deciding factor hai runtime ka fraction aapne kitna cut kiya, unaccelerated remainder se cap hoke — kernel-only multiplier nahi. Yahi parent note ka "benefit = frequency × cycles saved − cost, Amdahl-limited" concrete bana hai. Same reasoning chip scale par dekhne ke liye Hardware accelerators vs general-purpose CPUs aur Domain-specific architectures dekhein.

Recall Solution — L5.2

Unka core custom-0 decode karta hai, uske liye koi functional unit nahi pata, aur illegal-instruction trap raise karta hai. Program ruk jaata hai (ya OS trap handle karta hai) — yeh silently koi alag operation execute nahi karta. Yahi safety guarantee hai: ek unimplemented custom op trap karta hai, kabhi galat result nahi deta. Standard binaries ki portability untouched rehti hai, kyunki standard software kabhi custom-0..3 emit nahi karta. Yahan fragmentation purely ek toolchain matter hai (har core ko apne intrinsics/assembler support chahiye), correctness break nahi — bilkul parent note mein steel-man ki tarah.


Connections